将 Google Analytics cookie 转换为本地/会话存储 [英] Convert Google Analytics cookies to Local/Session Storage
问题描述
更新http://jsfiddle.net/musicisair/rsKtp/embedded/result/><小时>
Google Analytics 设置了 4 个 cookie,这些 cookie 将与所有请求一起发送到该域(并抵消其子域).据我所知,没有服务器实际直接使用它们;它们仅以 __utm.gif
作为查询参数发送.
现在,显然 Google Analytics 读取、写入它们的值并对其执行操作,它们需要可供 GA 跟踪脚本使用.
所以,我想知道是否有可能:
- 在
ga.js
写入它们后将__utm*
cookie 重写到本地存储 - 在
ga.js
运行后删除它们 - 在
ga.js
读取它们之前,将本地存储中的 cookie 重写回 cookie 形式 - 重新开始
或者,猴子补丁 ga.js
在开始 cookie 读/写部分之前使用本地存储.
显然,如果我们不打算删除 __utm*
cookie,我们还希望使用 Analytics 的异步变体.
我猜投反对票是因为我没有提出问题.卫生!
我的问题是:
可以按照上面的方法完成吗?
如果是这样,为什么还没有完成?
我有一个默认的 HTML/CSS/JS
样板模板,它以近乎完美的分数通过了 YSlow、PageSpeed 和 Chrome 的审核.我真的在寻找一种方法,可以在支持本地存储的浏览器中从 Google Analytics 中提取剩余的 cookie 字节.
使用这个:
(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');如果(窗口.本地存储){ga('创建', 'UA-98765432-1', 'www.example.com', {'存储':'无', 'clientId': window.localStorage.getItem('ga_clientId')});ga(功能(跟踪器){window.localStorage.setItem('ga_clientId', tracker.get('clientId'));});}别的 {ga('创建', 'UA-98765432-1', 'www.example.com');}ga('发送', '浏览量');
首先,我检查是否支持 localStorage
.如果支持,则 'storage': 'none'
选项将禁用 cookie.现在我们可以从 localStorage 设置 clientId.如果它是空的,谷歌分析会为我们生成一个新的.跟踪器加载后,我们将新的(或现有的)clientid 保存在 localStorage 中.
如果不支持 localStorage
,我只使用常规分析方法.初始化后,我通过 ga('send', 'pageView')
发送一个 pageView.
另外,看看这个 plunk:http://plnkr.co/MwH6xwGK00u3CFOTzepK
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 afterga.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.
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
这篇关于将 Google Analytics cookie 转换为本地/会话存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!