使用谷歌分析跟踪PHP API的使用

时间:2022-02-08 14:29:46

I have an API on my webserver that accesses the data in the website's database. I am trying to figure out how to use google analytics to track the API usage. The clients accessing the API response will not be able to execute javascript.

我的webserver上有一个API可以访问网站数据库中的数据。我正在尝试找出如何使用谷歌分析来跟踪API的使用。访问API响应的客户端将无法执行javascript。

I have tried https://developers.google.com/analytics/devguides/collection/other/mobileWebsites to do server side, but since MY API cannot open any images, it will not work. Any Idea out there?

我尝试过https://developers.google.com/analytics/devguides/collection/other/mobileWebsites来做服务器端,但是因为我的API不能打开任何图像,所以它不能工作。知道了吗?

API example is http://www.serviidb.com/api/video .

API示例是http://www.serviidb.com/api/video。

3 个解决方案

#1


5  

If you have updated to Universal Analytics (the new Google Analytics) you can generate events or pageviews directly from PHP.

如果您已经更新到Universal Analytics(新的谷歌分析),您可以直接从PHP生成事件或页面视图。

Google has created the Measurement protocol that is basically a set of http requests with some parameters contatining what you want to track. I will paste here the code I use in my own site to track downloads.

谷歌已经创建了度量协议,该协议基本上是一组http请求,其中有些参数与您想要跟踪的内容相冲突。我将在这里粘贴我在我自己的站点中用来跟踪下载的代码。

To track anything you just have to call AnalyticsDoHit() with your own parameters:

要跟踪任何内容,只需使用自己的参数调用AnalyticsDoHit():

// Create a page view directly from PHP. No javascript.
function AnalyticsDoHit($tid, $slug, $title)
{
// Standard params
$v   = 1;
$cid = ParseOrCreateAnalyticsCookie();

// Send PageView hit
$data = array(
    'v' => $v,
    'tid' => $tid,
    'cid' => $cid,
    't' => 'pageview',
    'dt' => $title,
    'dp' => $slug
);

$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
file_get_contents($getString); // do the https request
}


// Gets the current Analytics session identifier or create a new one
// if it does not exist
function ParseOrCreateAnalyticsCookie()
{
if (isset($_COOKIE['_ga']))
    {
    // An analytics cookie is found
    list($version, $domainDepth, $cid1, $cid2) = preg_split('[\.]', $_COOKIE["_ga"], 4);
    $contents = array(
        'version' => $version,
        'domainDepth' => $domainDepth,
        'cid' => $cid1 . '.' . $cid2
    );
    $cid      = $contents['cid'];
    }
else
    {
    // no analytics cookie is found. Create a new one
    $cid1 = mt_rand(0, 2147483647);
    $cid2 = mt_rand(0, 2147483647);

    $cid = $cid1 . '.' . $cid2;
    setcookie('_ga', 'GA1.2.' . $cid, time() + 60 * 60 * 24 * 365 * 2, '/');
    }
return $cid;
}

Use it as follows:

使用如下:

AnalyticsDoHit("UA-XXXXXX-X", "http://www.AutomatedEmailParser.com/EmailAndParser_setup.msi", "EmailAndParser_setup.msi");

#2


1  

Look at Universal Analytics https://developers.google.com/analytics/devguides/collection/protocol/v1/ Or http://code.google.com/p/php-ga/ for emulating ga.js server side in PHP

查看Universal Analytics https://developers.google.com/analytics/devguides/collection/protocol/v1/或http://code.google.com/p/php-ga/来模拟ga。PHP中的js服务器端

#3


-1  

The one I use is Google Analytics Measurement Protocol library for PHP, which has wrappers for many frameworks like Laravel, Yii, etc.

我使用的是用于PHP的谷歌分析度量协议库,它有许多框架的包装,如Laravel、Yii等。

You can measure things like API usage, e-commerce events, or whatever you need. You can also send async requests.

您可以度量API使用、电子商务事件或任何您需要的东西。您还可以发送异步请求。

#1


5  

If you have updated to Universal Analytics (the new Google Analytics) you can generate events or pageviews directly from PHP.

如果您已经更新到Universal Analytics(新的谷歌分析),您可以直接从PHP生成事件或页面视图。

Google has created the Measurement protocol that is basically a set of http requests with some parameters contatining what you want to track. I will paste here the code I use in my own site to track downloads.

谷歌已经创建了度量协议,该协议基本上是一组http请求,其中有些参数与您想要跟踪的内容相冲突。我将在这里粘贴我在我自己的站点中用来跟踪下载的代码。

To track anything you just have to call AnalyticsDoHit() with your own parameters:

要跟踪任何内容,只需使用自己的参数调用AnalyticsDoHit():

// Create a page view directly from PHP. No javascript.
function AnalyticsDoHit($tid, $slug, $title)
{
// Standard params
$v   = 1;
$cid = ParseOrCreateAnalyticsCookie();

// Send PageView hit
$data = array(
    'v' => $v,
    'tid' => $tid,
    'cid' => $cid,
    't' => 'pageview',
    'dt' => $title,
    'dp' => $slug
);

$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
file_get_contents($getString); // do the https request
}


// Gets the current Analytics session identifier or create a new one
// if it does not exist
function ParseOrCreateAnalyticsCookie()
{
if (isset($_COOKIE['_ga']))
    {
    // An analytics cookie is found
    list($version, $domainDepth, $cid1, $cid2) = preg_split('[\.]', $_COOKIE["_ga"], 4);
    $contents = array(
        'version' => $version,
        'domainDepth' => $domainDepth,
        'cid' => $cid1 . '.' . $cid2
    );
    $cid      = $contents['cid'];
    }
else
    {
    // no analytics cookie is found. Create a new one
    $cid1 = mt_rand(0, 2147483647);
    $cid2 = mt_rand(0, 2147483647);

    $cid = $cid1 . '.' . $cid2;
    setcookie('_ga', 'GA1.2.' . $cid, time() + 60 * 60 * 24 * 365 * 2, '/');
    }
return $cid;
}

Use it as follows:

使用如下:

AnalyticsDoHit("UA-XXXXXX-X", "http://www.AutomatedEmailParser.com/EmailAndParser_setup.msi", "EmailAndParser_setup.msi");

#2


1  

Look at Universal Analytics https://developers.google.com/analytics/devguides/collection/protocol/v1/ Or http://code.google.com/p/php-ga/ for emulating ga.js server side in PHP

查看Universal Analytics https://developers.google.com/analytics/devguides/collection/protocol/v1/或http://code.google.com/p/php-ga/来模拟ga。PHP中的js服务器端

#3


-1  

The one I use is Google Analytics Measurement Protocol library for PHP, which has wrappers for many frameworks like Laravel, Yii, etc.

我使用的是用于PHP的谷歌分析度量协议库,它有许多框架的包装,如Laravel、Yii等。

You can measure things like API usage, e-commerce events, or whatever you need. You can also send async requests.

您可以度量API使用、电子商务事件或任何您需要的东西。您还可以发送异步请求。