谷歌分析代理 [英] Google Analytics proxy

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

问题描述

我有一个特殊情况,网站访问者可以从某个域访问页面,但不能从其他域访问.所以HTML和assets只要存储在服务器上就没有问题.另一方面,Google Analytics 需要从 Google 服务器下载 analytics.js,这是不可能的.

所以我正在寻找一种方法来代理它.网络服务器本身可以访问互联网并且可以中继流量.为了向 Google 报告我的页面浏览量,从 Google 下载了一个单像素 GIF,描述如下:https://developers.google.com/analytics/resources/concepts/gaConceptsTrackingOverview

我认为获取 GIF 中的所有参数并使用测量协议从服务器向 Google 报告会很容易 - 但难点是将所有这些信息发送到服务器.下载analytics.js 并修改它以转到我自己的服务器在我看来是一种根本无法证明未来的黑客行为.仅将当前页面从用户获取到服务器并不是什么大问题,但我们希望获取用户 ID、浏览器版本以及您通过 Analytics 获得的所有信息.

你会怎么做?您找到解决方案了吗?

解决方案

更新:Google 已经发布了 服务器端 GTM,它允许您通过自定义域代理请求和脚本.在我可以想象的大多数用例中,这将是比 dyi 代理更优越的解决方案.


正如我在评论中指出的那样,不再使用 utm.gif.Google Analytics 已完全切换到测量协议,数据现在发送到 google-analytics.com/collect 上的测量协议端点.实际上这仍然返回一个透明像素,因为调用带有参数的图像是跨域边界传输信息的一种证明方式.

现在,您可以仅使用测量协议来实现您自己的 Google Analytics 跟踪器.

引用自己的话:

<块引用>

每次通话至少包含您要发送的帐户的ID数据到,允许将交互分组到会话中的客户端 ID(所以它应该是每个访问者唯一的,但它不能识别用户个人)、交互类型(浏览量、事件、时间等,一些交互类型需要额外的参数)和版本您使用的协议(目前只有一个版本).

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

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

现在您有了代码,但跟踪调用仍将发送到分析服务器(即在您的情况下根本不会发送).因此您需要通过您的服务器重新路由呼叫.

为了使这成为可能,Google(通用)分析代码有一个功能称为任务". 任务是跟踪代码中的函数,跟踪调用正在其中组合.

可以使用set"来修改任务.跟踪器对象的函数,使用任务名称作为参数并传递一个覆盖/重载任务函数的函数.

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

ga('create', 'UA-XXXXX-Y', 'auto');ga(功能(跟踪器){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('发送', '浏览量');

现在这会将数据发送到您自己服务器上名为 __ua.gif 的文件中(如果您需要跨域发送数据,您可以简单地执行 var ua = new Image; ua.src = gifPath + '?' +payLoad 创建图像请求).

sendHitTask 函数的模型参数包含(除了大量开销)有效负载,即包含分析数据的组合查询字符串.然后,您可以将 _ua.gif 设为将请求代理到 google-analytics.com/collect 的脚本.

此时用户代理将是您的脚本,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?

解决方案

Update: Google has since released server-side GTM, which allows you to proxy requests and scripts through a custom domain. In most use cases I can imagine, this would be the much superior solution to a dyi proxy.


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.

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

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