有没有办法通过服务器端API将事件发布到Google Analytics?

时间:2021-07-31 15:51:59

I'm trying to use Google Analytics from our backend system by posting events to it. Is there any way to do this with GA's API on server-side?

我正在尝试通过向其发布事件从我们的后端系统使用Google Analytics。有没有办法在服务器端使用GA的API?

6 个解决方案

#1


194  

It is now possible (and easy) to track Analytics data from the server-side. With the launch of Universal Analytics, you can now use the Measurement Protocol to post data to the GA servers.

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

Code samples here

代码示例在这里

#2


24  

Yes, Use the new Google Analytics Measurement Protocol

是的,使用新的Google Analytics测量协议

#3


14  

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();

        }
    }

#4


7  

If you use PHP you can easily call the Analytics Measurement Protocol to send page views to you Google Analytics account:

如果您使用PHP,则可以轻松调用Analytics Measurement Protocol将页面视图发送给您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');

Hope that helps!

希望有所帮助!

#5


1  

If you are not ready to upgrade to Google's Universal Analytics and for those looking for a Ruby (and/or Rails) solution, take a look at the Gabba gem.

如果您尚未准备好升级到Google的Universal Analytics以及那些寻求Ruby(和/或Rails)解决方案的人,请查看Gabba gem。

https://github.com/hybridgroup/gabba

https://github.com/hybridgroup/gabba

Non-ruby folks might also look at this code for some examples on how to (generally) implement a server side solution for GA.

对于如何(通常)为GA实现服务器端解决方案的一些示例,非ruby人员也可能会查看此代码。

#6


1  

Take a look at the usage-stats module.

看一下usage-stats模块。

Command line

Tracking statistics in shell scripts:

跟踪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

The most trivial example.

最琐碎的例子。

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

usageStats.screenView('screen name')
usageStats.event('category', 'action')
usageStats.send()

#1


194  

It is now possible (and easy) to track Analytics data from the server-side. With the launch of Universal Analytics, you can now use the Measurement Protocol to post data to the GA servers.

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

Code samples here

代码示例在这里

#2


24  

Yes, Use the new Google Analytics Measurement Protocol

是的,使用新的Google Analytics测量协议

#3


14  

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();

        }
    }

#4


7  

If you use PHP you can easily call the Analytics Measurement Protocol to send page views to you Google Analytics account:

如果您使用PHP,则可以轻松调用Analytics Measurement Protocol将页面视图发送给您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');

Hope that helps!

希望有所帮助!

#5


1  

If you are not ready to upgrade to Google's Universal Analytics and for those looking for a Ruby (and/or Rails) solution, take a look at the Gabba gem.

如果您尚未准备好升级到Google的Universal Analytics以及那些寻求Ruby(和/或Rails)解决方案的人,请查看Gabba gem。

https://github.com/hybridgroup/gabba

https://github.com/hybridgroup/gabba

Non-ruby folks might also look at this code for some examples on how to (generally) implement a server side solution for GA.

对于如何(通常)为GA实现服务器端解决方案的一些示例,非ruby人员也可能会查看此代码。

#6


1  

Take a look at the usage-stats module.

看一下usage-stats模块。

Command line

Tracking statistics in shell scripts:

跟踪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

The most trivial example.

最琐碎的例子。

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

usageStats.screenView('screen name')
usageStats.event('category', 'action')
usageStats.send()