Google Analytics代理 [英] Google Analytics proxy

查看:123
本文介绍了Google Analytics代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种特殊情况,网站访问者可以从某个域名访问该页面,但不能访问其他域名。因此,HTML和资产只要存储在服务器上就没有问题。另一方面,谷歌分析需要从谷歌的服务器下载analytics.js,这是不可能的。

所以我正在寻找一种代理方式。网络服务器本身具有互联网接入功能,可以中继交通。要向Google报告我的网页浏览量,可以从Google下载一个像素GIF,如下所述: https://developers.google.com/analytics/resources/concepts/gaConceptsTrackingOverview



我认为这将是一种容易得到的所有参数GIF并使用测量协议从服务器向Google报告 - 但最难的是将所有这些信息都传送到服务器。要下载analytics.js并修改它以转到我自己的服务器,在我看来,它是一种根本不能作为未来证明的黑客。要将当前页面从用户移到服务器上并不是什么大事,但我们希望获得用户ID,浏览器版本以及您通过Google Analytics获得的所有内容。



你会怎么做?你是否找到了解决方案?

解决方案

正如我的评论所指出的,utm.gif不再使用。谷歌分析已完全切换到测量协议,现在数据已发送到测量协议的端点,网址为google-analytics.com/collect。事实上,这仍然返回一个透明的像素,因为调用带参数的图像是跨域传输信息的遗产方式。

现在,您可以使用Measurement Protocol来实现您的自己的Google Analytics(分析)追踪程式。

引述我自己


每次通话至少包含您要发送的帐号的ID
数据到一个客户端ID,它允许将交互分组到会话
(所以它应该对每个访问者都是唯一的,但它不能确定一个用户
个人),交互类型(pageview,event,timing等。,一些
交互类型需要额外的参数)和
版本的协议(目前只有一个版本)。



<因此,记录综合浏览量的最基本的例子如下所示:



www.google-analytics.com/collect/v=1&tid=UA-XXXXY&cid=555&t=pageview&dp=%2Fmypage


您可能希望添加用户IP(将自动匿名)和用户代理。然而,听起来您更喜欢使用标准Google Analytics代码来收集数据,并通过您自己的服务器转播跟踪呼叫。虽然我没有在生产中使用以下内容,但我没有看到任何原因导致无法正常工作。



首先,您需要analytics.js文件。自行托管文件是不鼓励的,但原因在于代码有时由Google更新,如果您自己托管它,则可能会错过更新。这可以通过设置一个定期将文件下载到您的服务器的cron作业来解决,因此您始终拥有当前版本。

接下来,您将修改GA引导程序功能从你自己的服务器加载代码:

 (function(i,s,o,g,r,a,m) {i ['GoogleAnalyticsObject'] = r; i [r] = i [r] || function(){
(i [r] .q = i [r] .q || [])。push (参数)},i [r] .l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a .src = g; m.parentNode.insertBefore(a,m)
})(window,document,'script','// www.myserver.com/analytics.js','ga');

现在您已拥有该代码,但追踪呼叫仍会发送至Analytics Server你的情况根本不会被发送)。因此,您需要通过您的服务器重新路由通话。



为了实现这一目标,Google(通用)分析代码有一个称为任务的功能。任务是跟踪代码中的函数,其中正在汇编跟踪调用。 / p>

可以通过使用跟踪器对象的set函数来修改任务,使用taskname作为参数并传递覆盖/重载任务函数的函数。 / p>

以下几乎是Google文档中的示例(除了我省略了仍然向Google发送数据的部分 - 此时您不需要此部分) :

  ga('create','UA-XXXXX-Y','auto'); 
$ b $ ga(function(tracker){

tracker.set('sendHitTask',function(model){
var payLoad = model.get('hitPayload' );
var gifRequest = new XMLHttpRequest();
var gifPath =/__ua.gif;
gifRequest.open('get',gifPath +'?'+ payLoad,true) ;
gifRequest.send();
});
});

ga('send','pageview');

现在这会将数据发送到您自己的服务器上的名为__ua.gif的文件(如果您需要发送数据跨域,你可以简单地做一个var ua = new Image; ua.src = gifPath +'?'+ payLoad来创建一个图片请求)。对于 sendHitTask -function的模型参数包含(除了大量开销外)有效负载,即包含分析数据的组装查询字符串。然后,您可以使用_ua.gif脚本将请求代理到google-analytics.com/collect。

此时用户代理将是您的脚本,IP地址将是您服务器的IP地址,因此您需要包含& uip(用户IP覆盖)和& ua(用户代理覆盖)参数( https://groups.google.com/forum/#!msg/google-analytics-measurement-protocol/8TAp7_I1uTk/KNjI5IGwT58J )以获取地理位置和技术信息。



如果您觉得更冒险,您可以覆盖 buildHitTask ,然后在其中添加额外的参数(更多很可能因为你需要从某处获得IP地址)。

有关其他参数,请参阅 analytics.js 测量协议


I have a special situation where the sites visitors can access the page from a certain domain but no others. So HTML and assets are no problem as long as they are stored on the server. Google Analytics on the other hand requires a download of analytics.js from Googles servers, which is impossible.

So I'm looking for a way to proxy this. The webserver itself has internet access and could relay the trafic. To report to Google about my page view, a single pixel GIF is downloaded from Google, described here: https://developers.google.com/analytics/resources/concepts/gaConceptsTrackingOverview

I think it would be kind of easy to get all the parameters in the GIF and use the measurement protocol to report to Google from the server - but the hard bit is to get all this info to the server. To download analytics.js and modify it to go to my own server seems to me as a hack that ain't future proof at all. To just get the current page from the user to the server is not a big deal, but we would like to get the user id, browser version and everything you get with Analytics.

How would you do it? Do you find a solution for this?

解决方案

As pointed out in my comment the utm.gif is no longer used. Google Analytics has completely switched to the Measurement Protocol and data is now sent to the Endpoint for the Measurement Protocol at google-analytics.com/collect. Actually this still return a transparent pixel since calling an image with parameters is a probate way of transmitting informations across domain boundaries.

Now, you could just the Measurement Protocol to implement your own Google Analytics tracker.

To quote myself:

Each calls includes at least the ID of the account you want to send data to, a client id that allows to group interactions into sessions (so it should be unique per visitor, but it must not identify a user personally), an interaction type (pageview, event, timing etc., some interactions types require additional parameters) and the version of the protocol you are using (at the moment there is only one version).

So the most basic example to record a pageview would look like this:

www.google-analytics.com/collect/v=1&tid=UA-XXXXY&cid=555&t=pageview&dp=%2Fmypage

You probably would want to add the users IP (will be anonymized automatically) and the user agent.

However it sounds like you prefer to use the standard Analytics code to collect the data and relay the tracking call via your own server. While I haven't used the following in production I don't see any reason why it wouldn't work.

First you need the analytics.js file. Self-hosting the file is discouraged, but the given reason is that the code is updated sometimes by Google and if you host it yourself you might miss the updates. This can be remedied by setting up a cron job that downloads the file regularly to your server so you always have a current version.

Next you'd adapt the GA bootstrap function to load the code from your own server:

  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.myserver.com/analytics.js','ga');

Now you have the code, but the tracking call will still be sent to the Analytics Server (i.e. in your case it won't be sent at all). So you need to re-route the call via your server.

To make this possible the Google (Universal) Analytics Code has a feature called "tasks". Tasks are functions within the tracking code in which the tracking call is being assembled.

It is possible to modify tasks by using the "set" function of the tracker object, using the taskname as parameter and passing a function that overwrites/overloads the task function.

The following is pretty much the example from the Google documentation (except I omitted the part where data is still being sent to Google - you don't need this at this point):

ga('create', 'UA-XXXXX-Y', 'auto');

ga(function(tracker) {

  tracker.set('sendHitTask', function(model) {
    var payLoad = model.get('hitPayload');
    var gifRequest = new XMLHttpRequest();
    var gifPath = "/__ua.gif";
    gifRequest.open('get', gifPath + '?' + payLoad, true);
    gifRequest.send();
  });
});

ga('send', 'pageview');   

Now this sends the data to a file called __ua.gif at your own server (if you need to send data cross-domain you can simply do a var ua = new Image; ua.src = gifPath + '?' + payLoad to create an image request).

The model parameter to the sendHitTask-function contains (apart from a lot of overhead) the payload, that is the assembled query string that contains the analytics data. You can then make your _ua.gif a script that proxies the request to the google-analytics.com/collect.

At this point the user agent will be your script and the IP adress will be that of your server, so you need to include &uip (User IP override) and &ua (User agent override) parameters ( https://groups.google.com/forum/#!msg/google-analytics-measurement-protocol/8TAp7_I1uTk/KNjI5IGwT58J) to get geo and technical information.

If you are feeling more adventurous you can override the buildHitTask instead and try and add the additional parameters there (more hassle probably since you'd need to get the IP address from somewhere).

For additional parameter see the reference for analytics.js and the Measurement Protocol.

这篇关于Google Analytics代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆