将 Google Analytics cookie 转换为本地/会话存储

发布于 2024-10-09 02:13:17 字数 1003 浏览 0 评论 0原文

更新 http://jsfiddle.net/musicisair/rsKtp/embedded/result/


Google Analytics(分析)设置 4 个 cookie,这些 cookie 将随所有请求一起发送到该域(并偏移其子域)。据我所知,没有服务器实际上直接使用它们;它们仅与 __utm.gif 作为查询参数一起发送。

现在,显然 Google Analytics 会读取、写入它们的值并对其进行操作,并且它们需要可供 GA 跟踪脚本使用。

所以,我想知道是否可以:

  • 在 ga.js 写入后将 __utm* cookie 重写到本地存储,并
  • 在 ga 后删除它们。 ,Node.js 已将
  • cookie 从本地存储重写回 cookie 形式
  • ga.js 重新读取它们之前

,或者,monkey patch ga.js在开始 cookie 读/写部分之前使用本地存储。

显然,如果我们不遗余力地删除 __utm* cookie,我们还希望使用Analytics 的异步变体。

我猜投反对票是因为我没有提出问题。卫生部!

我的问题是:
可以按照上面的方法做吗?
如果是这样,为什么还没有完成?


我有一个默认的 HTML/CSS/JS 样板模板,它以接近完美的分数通过了 YSlow、PageSpeed 和 Chrome 的审核。我确实在寻找一种方法,在支持本地存储的浏览器中从 Google Analytics 中压缩剩余的 cookie 字节。

UPDATE
http://jsfiddle.net/musicisair/rsKtp/embedded/result/


Google Analytics sets 4 cookies that will be sent with all requests to that domain (and ofset its subdomains). From what I can tell no server actually uses them directly; they're only sent with __utm.gif as a query param.

Now, obviously Google Analytics reads, writes and acts on their values and they will need to be available to the GA tracking script.

So, what I am wondering is if it is possible to:

  • rewrite the __utm* cookies to local storage after ga.js has written them
  • delete them after ga.js has run
  • rewrite the cookies FROM local storage back to cookie form right before ga.js reads them
  • start over

Or, monkey patch ga.js to use local storage before it begins the cookie read/write part.

Obviously if we are going so far out of the way to remove the __utm* cookies we'll want to also use the Async variant of Analytics.

I'm guessing the down vote was because I didn't ask a question. DOH!

My questions are:
Can it be done as described above?
If so, why hasn't it been done?


I have a default HTML/CSS/JS boilerplate template that passes YSlow, PageSpeed, and Chrome's Audit with near perfect scores. I'm really looking for a way to squeeze those remaining cookie bytes from Google Analytics in browsers that support local storage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-10-16 02:13:17

使用这个:

(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.google-analytics.com/analytics.js','ga');

if(window.localStorage) {
    ga('create', 'UA-98765432-1', 'www.example.com', {
      'storage': 'none'
      , 'clientId': window.localStorage.getItem('ga_clientId')
    });
    ga(function(tracker) {
      window.localStorage.setItem('ga_clientId', tracker.get('clientId'));
    });
}
else {
    ga('create', 'UA-98765432-1', 'www.example.com');
}
ga('send', 'pageview');

首先,我检查是否支持 localStorage。如果支持,则 'storage': 'none' 选项将禁用 cookie。现在我们可以从 localStorage 设置 clientId。如果它是空的,Google Analytics 将为我们生成一个新的。跟踪器加载后,我们将新的(或现有的)clientid 保存在 localStorage 中。

如果不支持 localStorage,我就使用常规分析方法。初始化后,我通过 ga('send', 'pageView') 发送 pageView。

另外,看看这个 plunk:http://plnkr.co/MwH6xwGK00u3CFOTzepK

Use this:

(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.google-analytics.com/analytics.js','ga');

if(window.localStorage) {
    ga('create', 'UA-98765432-1', 'www.example.com', {
      'storage': 'none'
      , 'clientId': window.localStorage.getItem('ga_clientId')
    });
    ga(function(tracker) {
      window.localStorage.setItem('ga_clientId', tracker.get('clientId'));
    });
}
else {
    ga('create', 'UA-98765432-1', 'www.example.com');
}
ga('send', 'pageview');

First, I check if localStorage is supported. If it is supported then the 'storage': 'none' option will disable cookies. Now we can set the clientId from localStorage. If it is empty, Google Analytics will generate a new one for us. We save the new (or existing) clientid in localStorage after the tracker loads.

If localStorage is not supported, I just use the regular analytics method. After the initialization I send a pageView via ga('send', 'pageView').

Also, check out this plunk: http://plnkr.co/MwH6xwGK00u3CFOTzepK

歌枕肩 2024-10-16 02:13:17

Chrome 中的一些实验表明,可以使用 getter 和 setter 来为此修补 document.cookie,例如:

document.__defineGetter__('cookie', function () {
    // Replace this with code to read from localstorage
    return "hello";
});
document.__defineSetter__('cookie', function (value) {
    // Replace this with code to save to localstorage
    console.log(value);
});

ga.js(或任何其他 javascript)可以正常运行和访问 cookie,它们永远不会被传递到服务器。

显然这仅适用于某些浏览器。它不起作用的浏览器将不得不回退到正常的 cookie。

这个问题有一些相关的想法: Is it possible to mock document JavaScript 中的 .cookie?

Some experimentation in chrome shows that it may be possible to use getters and setters to patch document.cookie for this, something like:

document.__defineGetter__('cookie', function () {
    // Replace this with code to read from localstorage
    return "hello";
});
document.__defineSetter__('cookie', function (value) {
    // Replace this with code to save to localstorage
    console.log(value);
});

ga.js (or any other javascript) could run and access cookies as normal, they just would never get passed to the server.

Obviously this will only work in some browsers. Browsers in which it doesn't work will have to fall back to normal cookies.

There's some related ideas in this question: Is it possible to mock document.cookie in JavaScript?

云裳 2024-10-16 02:13:17

是的,这是可以做到的。您只需请求带有参数的__utm.gif。其余数据仅用于跟踪来源、会话开始时间和/或之前的访问。

您可以轻松地双向传输 cookie,因此您的第一种方法应该可以正常工作。

如果你的第二种方法有效......不确定。我不知道 ga.js 代码是否足够好来估计这是否容易实现。

还有第三个选项,运行您自己的 ga.js 版本。您不需要使用 Google 版本。

可以按照上面描述的方式完成吗?
是的

为什么还没有这样做?

  1. cookie 很小,如果您对所有静态内容使用 cookieless 域,那么并没有多大好处
  2. ,因为很多浏览器不支持,所以不太方便还没有

Yes it can be done. You only have to request the __utm.gif with the parameters. The rest of the data is just used for keeping track of the source, session start time and/or previous visits.

You can easily transfer the cookies both ways, so your first approach should work fine.

If your second approach works... not sure. I don't know the ga.js code good enough to estimate wheter that would or would not be easily possible.

There is also a third option, run your own version of ga.js. You are not required to use the Google version.

Can it be done as described above?
Yes

Why hasn't it been done?

  1. the cookies are small, there isn't that much benefit if you use cookieless domains for all your static content
  2. it's less convenient since a lot of browsers don't support it yet
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文