piwik custom variables
|
piwikcustom variables 是一个功能非常强大的自定义变量跟踪方案,多用于基于访客或是页面级别的变量跟踪。piwik默认最多可以添加5个自定义变量。 使用方式是在客户端脚本里添加如下片断 _paq.push(['setCustomVariable', 1, "Gender", "Male", "visit"查看setCustomVariable源码如下 : (index,name,value,</span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">isDefined(scope)) {
scope </span>= 'visit'<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">isDefined(name)) {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">isDefined(value)) {
value </span>= ""<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">if</span> (index > 0<span style="color: #000000;">) {
name </span>= !isString(name) ? <span style="color: #0000ff;">String</span>(name) :<span style="color: #000000;"> name;
value </span>= !isString(value) ? <span style="color: #0000ff;">String</span>(value) :<span style="color: #000000;"> value;
toRecord </span>= [name.slice(0,customVariableMaximumLength),value.slice(0,<span style="color: #000000;"> customVariableMaximumLength)];
</span><span style="color: #008000;">//</span><span style="color: #008000;"> numeric scope is there for GA compatibility</span>
<span style="color: #0000ff;">if</span> (scope === 'visit' || scope === 2<span style="color: #000000;">) {
loadCustomVariables();
customVariables[index] </span>=<span style="color: #000000;"> toRecord;
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (scope === 'page' || scope === 3<span style="color: #000000;">) {
customVariablesPage[index] </span>=<span style="color: #000000;"> toRecord;
} </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (scope === 'event') { <span style="color: #008000;">/*</span><span style="color: #008000;"> GA does not have 'event' scope but we do </span><span style="color: #008000;">*/</span><span style="color: #000000;">
customVariablesEvent[index] </span>=<span style="color: #000000;"> toRecord;
}
}
} 参数解释:
如果三个scope同时都设置了值也是可以的,因为piwik在发送请求时visit级别才用到的是_cvar参数,page是用的是cvar,而event才用的是e_cvar,例如下面的例子 _paq.push(['setCustomVariable',1,"GenderX","MaleX","visit".push(['setCustomVariable',"GenderY","MaleY","page".push(['setCustomVariable',"GenderZ","MaleZ","event".push(['trackPageView']);请求发送的参数是 cvar={"1":["GenderY","MaleY"]}&e_cvar={"1":["GenderZ","MaleZ"]}&_cvar={"1":["GenderX","MaleX"]} 当然index是可以改变的,范围1-5,这里只是为了演示piwik针对相同index不同级别的scope是如何处理的而已。 删除一个自定义变量请使用下面的方法 _paq.push(['deleteCustomVariable',"visit"]);获取自定义变量 customVariable = this.getCustomVariable( 1,"visit" );如果自定义变量不存在会返回false 另外在使用visit级别的时候,如果要在跨页面或是页面刷新后能获取之前设置的变量,需要在 _paq.push(['trackPageView']);前添加下面的代码 _paq.push(['storeCustomVariablesInCookie']);这是因为visit跨页面访问自定义变量是通过存储cookie来是实现的,只有把变量存储到了cookie,在其他的页面或是刷新后才能通过getCustomVariable来获取那个设置的变量。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
