使用 Google Analytics 跟踪内容脚本 [英] Content Script tracking with Google Analytics

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

问题描述

我正在开发一个使用内容脚本修改某些页面的 Google Chrome 扩展程序.为了了解何时何地应用这些更改,我们正在研究谷歌分析,因为我们已经将其用于其他网络资产.

我们通过清单文件包含 ga.js 库,并在我们对页面应用更改时通过同步页面调用触发 _trackPageview.

var pageTracker = _gat._getTracker("UA-our-UA");pageTracker._setDomainName("none");pageTracker._setAllowLinker(true);pageTracker._trackPageview(window.location.href);

这里的问题是,不同域中的每个 pageView 都算作一次新访问和访问者.它有办法规避吗?例如,它在浏览器中使用 chrome 扩展 sqlite 数据库或键值存储来存储唯一的用户 id 并独立访问 id 域.这可能吗?

谢谢

解决方案

您不应该在其他人的网站内执行该代码.Google Analytics 配置有些敏感,如果网站有自定义实现,您可能会为访问者破坏它.

您应该在自己的背景页面中加入 Google Analytics.然后在每次需要跟踪事件时从 content_script 返回到后台页面.

通过在您的后台脚本中包含 GA 脚本很好,因为您不会干扰网站上的其他代码,并且它始终从同一域执行,因此将使用相同的 cookie,不会导致重复访问/访问者.

这里有更多关于如何在后台页面上安装 GA 的信息.

https://developer.chrome.com/docs/extensions/mv2/tut_analytics/

这里是将信息从内容脚本传递到背景页面的文档:

http://code.google.com/chrome/extensions/messaging.html

I am working on a Google Chrome Extension that modifies certain pages with a content script. In order to understand when and where those changes are applied we were looking into google analytics since we use it for other web properties already.

We include the ga.js library via the manifest file, and trigger _trackPageview with a synchronous page call when we apply a change to a page.

var pageTracker = _gat._getTracker("UA-our-UA");    
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);    
pageTracker._trackPageview(window.location.href);

Trouble here is that each pageView in a different domain counts as a new visit and visitor. Its there a way to circumvent that? For example it use a chrome extension sqlite database or key value store in the browser to store unique user id and visit id domain independent. Is this possible?

Thanks

解决方案

You should not execute that code inside other people sites. The Google Analytics configuration is somewhat sensitive and if a site has a custom implementation you might be breaking it for that visitor.

You should include Google Analytics in your own background page. And then communicate from the content_script back to your background page everytime you need to track an event.

By including the GA script on your background script is nice because you don't interfere with other code on the website and it always execute from the same domain and thus will use the same cookies not causing duplicated visits/visitors.

Here are more info on how to install GA on your background page.

https://developer.chrome.com/docs/extensions/mv2/tut_analytics/

And here's the docs for passing information from Content Scripts to your background page:

http://code.google.com/chrome/extensions/messaging.html

这篇关于使用 Google Analytics 跟踪内容脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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