是否可以通过服务器端API将事件发布到Google Analytics(分析)?[关闭]


178

我正在尝试通过在后端系统中发布事件来使用Google Analytics(分析)。有什么方法可以在服务器端使用GA的API做到这一点?


是的,使用像这样
Lloyd

Answers:


231

现在可以(并且很容易)从服务器端跟踪Analytics数据。随着Universal Analytics的推出,您现在可以使用Measurement Protocol将数据发布到GA服务器。

这里的代码示例


5
这应该被接受。我们可能会遇到类似OP的情况,我们希望通过网站访问者数据获得相当准确的收入数字。并且浏览器端跟踪对于完成付款的最后一步还不够好(例如,客户没有从付款提供商的站点回来)。
塔达斯(Tadas Sasnauskas)

我与客户有同样的问题,但不能从支付提供商的网站回来,但是我没有客户ID。如何将数据附加到客户端会话?
Korjavin Ivan 2014年

2
@KorjavinIvan这里以获取测量议定书规定的客户端ID或CID paramater说明: developers.google.com/analytics/devguides/collection/...
豪尔赫Pedret

1
这似乎是解决广告拦截器效果的好方法,广告拦截器会寻找ga.js之类的
引用

相同的Measurement Protocol,Google Analytics和Google Tag Manager
Kiquenet '19

21
using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

    public class GoogleAnalyticsApi
    {
        public static void TrackEvent(string type, string category,
               string action, string label, string value)
        {

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = 
                "v=1&tid=UX-XXXXXXX-1&cid=1234&t=" + type +
                "&ec=" + category + 
                "&ea=" + action + 
                "&el=" + label + 
                "&ev=" + value;
            byte[] data = encoding.GetBytes(postData);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.google-analytics.com/collect");

            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

        }
    }

这使用通用分析正确吗?
布雷迪·莫里兹

@BradyMoritz是的
Koby Douek

另外-在可以同时使用get请求时使用post的任何理由?
布雷迪·莫里兹

这是C#吗?您应该指定也许吗?
Esqarrouth

7

如果您使用PHP,则可以轻松调用Google Analytics(分析)衡量协议,以将页面浏览量发送给您的Google Analytics(分析)帐户:

function sendAnalytics($sGaId, $sHostname, $sPath, $sTitle) {

    $aParams = array();

    //Protocol Version
    $aParams['v'] = '1';

    //Tracking ID / Web Property ID
    $aParams['tid'] = $sGaId;

    //Anonymize IP
    $aParams['aip'] = '1';

    //Data Source
    $aParams['ds'] = 'web';

    //Queue Time
    $aParams['qt'] = 0;

    //Client ID
    $aParams['cid'] = substr(md5($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']), 0, 8);

    //User ID
    //$aParams['uid'] = '';

    //Session Control
    //$aParams[''] = '';

    //IP Override
    $aParams['uip'] = $_SERVER['REMOTE_ADDR'];

    //User Agent Override
    $aParams['ua'] = urlencode($_SERVER['HTTP_USER_AGENT']);

    //Geographical Override
    //$aParams['geoid'] = '';

    //Document Referrer
    //$aParams['dr'] = '';

    //Campaign Name
    //$aParams['cn'] = '';

    //Campaign Source
    //$aParams['cs'] = '';

    //Campaign Medium
    //$aParams['cm'] = '';

    //Campaign Keyword
    //$aParams['ck'] = '';

    //Campaign Content
    //$aParams['cc'] = '';

    //Campaign ID
    //$aParams['ci'] = '';

    //Google AdWords ID
    //$aParams['gclid'] = '';

    //Google Display Ads ID
    //$aParams[''] = '';


    ////SystemInfo => see docs

    //Hit type
    $aParams['t'] = 'pageview';

    //Non-Interaction Hit
    //$aParams['ni'] = '';

    //Hostname
    $aParams['dh'] = $sHostname;

    //Document Path
    $aParams['dp'] = $sPath;

    //Document title
    $aParams['dt'] = urlencode($sTitle);


    $sGaUrl = 'http://www.google-analytics.com/collect?';


    foreach($aParams AS $sKey => $sValue) {
        $sGaUrl.= "$sKey=$sValue&";
    }

    $sGaUrl = substr($sGaUrl, 0, -1);

    file_get_contents($sGaUrl);
}


sendAnalytics('UA-XXXXXXXX-1', 'http://foo.com', '/bar', 'Foo Bar');

希望有帮助!


1
http_build_query()可以使它更清洁(并支持适当的转义)
kainjow

2

看一下usage-stats模块。

命令行

在Shell脚本中跟踪统计信息:

# Track an event: category 'Backup', action 'start'
usage-stats event --tid UA-98765432-1 --ec Backup --ea start

# Perform the backup
cp files/** backup/

# Track an event: category 'Backup', action 'complete'
usage-stats event --tid UA-98765432-1 --ec Backup --ea complete

API

最琐碎的例子。

const UsageStats = require('usage-stats')
const usageStats = new UsageStats('UA-98765432-1', { an: 'example' })

usageStats.screenView('screen name')
usageStats.event('category', 'action')
usageStats.send()
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.