piwik custom variables
piwik custom variables 是一个功能非常强大的自定义变量跟踪方案,多用于基于访客或是页面级别的变量跟踪。piwik默认最多可以添加5个自定义变量。
使用方式是在客户端脚本里添加如下片断
_paq.push(['setCustomVariable',
// Index, the number from 1 to 5 where this custom variable name is stored
1,
// Name, the name of the variable, for example: Gender, VisitorType
"Gender",
// Value, for example: "Male", "Female" or "new", "engaged", "customer"
"Male",
// Scope of the custom variable, "visit" means the custom variable applies to the current visit
"visit"
]);
查看setCustomVariable源码如下
/**
* Set custom variable within this visit
*
* @param int index
* @param string name
* @param string value
* @param string scope Scope of Custom Variable:
* - "visit" will store the name/value in the visit and will persist it in the cookie for the duration of the visit,
* - "page" will store the name/value in the next page view tracked.
* - "event" will store the name/value in the next event tracked.
*/
setCustomVariable: function (index, name, value, scope) {
var toRecord; if (!isDefined(scope)) {
scope = 'visit';
}
if (!isDefined(name)) {
return;
}
if (!isDefined(value)) {
value = "";
}
if (index > 0) {
name = !isString(name) ? String(name) : name;
value = !isString(value) ? String(value) : value;
toRecord = [name.slice(0, customVariableMaximumLength), value.slice(0, customVariableMaximumLength)];
// numeric scope is there for GA compatibility
if (scope === 'visit' || scope === 2) {
loadCustomVariables();
customVariables[index] = toRecord;
} else if (scope === 'page' || scope === 3) {
customVariablesPage[index] = toRecord;
} else if (scope === 'event') { /* GA does not have 'event' scope but we do */
customVariablesEvent[index] = toRecord;
}
}
}
参数解释:
- index :表示使用5个自定义变量的中的第几个
- name:自定义变量名称
- value:自定义变量值
- scope:自定义变量的使用范围,包括:
- visit(访客级别):同一个访客30分钟内在不同的页面都能操作该变量,可以做计算
- page(页面级别):在当前页面只要页面没有重载就可以获得该变量,即通过别的js事件或是方法是可以获取到该变量的。
- event(事件级别):在当前页面的只能在一个事件级别里获取该变量,下次事件操作将清空改变量,此时页面并无重载操作。
需要注意的是:如果同一个scope的情况下,相同的index值如果先后被设置了不同的name,value,那么后设置的将会覆盖前面的。但是如果scope范围不同的话则无影响。
如果三个scope同时都设置了值也是可以的,因为piwik在发送请求时visit级别才用到的是_cvar参数,page是用的是cvar,而event才用的是e_cvar,例如下面的例子
_paq.push(['setCustomVariable',1,"GenderX","MaleX","visit"]);
_paq.push(['setCustomVariable',1,"GenderY","MaleY","page"]);
_paq.push(['setCustomVariable',1,"GenderZ","MaleZ","event"]);
/*cvar={"1":["GenderXXX","MaleXXX"]}&_cvar={"1":["Gender","Male"]}*/
_paq.push(['trackPageView']);
请求发送的参数是
cvar={"1":["GenderY","MaleY"]}&e_cvar={"1":["GenderZ","MaleZ"]}&_cvar={"1":["GenderX","MaleX"]}
当然index是可以改变的,范围1-5,这里只是为了演示piwik针对相同index不同级别的scope是如何处理的而已。
删除一个自定义变量请使用下面的方法
_paq.push(['deleteCustomVariable', 1, "visit"]);
获取自定义变量
var customVariable = this.getCustomVariable( 1, "visit" );
如果自定义变量不存在会返回false
另外在使用visit级别的时候,如果要在跨页面或是页面刷新后能获取之前设置的变量,需要在
_paq.push(['trackPageView']);
前添加下面的代码
_paq.push(['storeCustomVariablesInCookie']);
这是因为visit跨页面访问自定义变量是通过存储cookie来是实现的,只有把变量存储到了cookie,在其他的页面或是刷新后才能通过getCustomVariable来获取那个设置的变量。
piwik custom variables的更多相关文章
- Animation Blueprint, Set Custom Variables Via C++
https://wiki.unrealengine.com/Animation_Blueprint,_Set_Custom_Variables_Via_C%2B%2B Animation Bluepr ...
- 开源网站流量统计系统Piwik源码分析——参数统计(一)
Piwik现已改名为Matomo,这是一套国外著名的开源网站统计系统,类似于百度统计.Google Analytics等系统.最大的区别就是可以看到其中的源码,这正合我意.因为我一直对统计的系统很好奇 ...
- [UE4] Adding a custom shading model
转自:https://blog.felixkate.net/2016/05/22/adding-a-custom-shading-model-1/ This was written in Februa ...
- Code::Blocks项目配置基础
File 菜单 New :新建( Empty file/file . class . project . build target ) . Recent projects/files :近期打开的项目 ...
- pycharm 修改新建文件时的头部模板(默认为__author__='...')
pycharm 修改新建文件时的头部模板 默认为__author__='...' [省略号是默认你的计算机名] 修改这个作者名的步骤: 依次点击:File->Settings->Ed ...
- vscode restclient 插件
使用步骤: 1.vscode 安装restclient 扩展 2.创建 .http 或 .rest 文件 ,编写相应内容 同一个文件内 可以通过 ### 分割多个请求 可以通过 @hostname ...
- Mobile CI/CD 101
This is a guest post by Slava Chernikoff, Principal Engineer at Binwell. Mobile DevOps falls under t ...
- CentOS下配置SS5(SOCKS5)代理服务器
方案:使用开源的SS5( Socks Server 5 ) 官网:http://ss5.sourceforge.net/ (点击左侧的Software在右侧的Server处进入下载地址) CentOs ...
- [Java in NetBeans] Lesson 04. Class / Objects
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...
随机推荐
- Python基础3--Python复杂数据类型
1 堆 堆是一种二叉树,其中每个父节点的值都小于或等于其所有子节点的值,最小的元素总是位于二叉树的根节点. 堆的创建 import heapq import random data = range(1 ...
- 1--Selenium环境准备--Eclipse 添加Testng插件
Eclipse安装TestNG TestNG官网地址:http://testng.org/ 1.离线安装TestNG插件: 受网络等因素影响,在线安装方式速度比较慢,可以通过如下方式离线安装TestN ...
- navicat下载安装和激活一分钟完成
下载navicat安装包和注册机 下载地址:https://pan.baidu.com/s/1Nakfuv7Z__vLiY6sHNusNg 提取码:v4gz 安装navicat 软件 以管 ...
- 关于DDOS的主动与智能防御
- 善守者藏于九地之下 - - 狡兔九窟 - 一.分配足够多的网关服务器 让用户总一个终点,可以进入游戏.多分配,动态分配,定期更新 二.用户分组分级 分组可以根据用户的生成时间, 在线时 ...
- maven 细节 —— scope、坐标
对于 idea 开发环境,测试代码便是在 src/test/java(该java目录会在创建时标注为测试文件夹) 目录下的 .java 代码为测试代码: 1. scope scope的分类 compi ...
- e与复利
e≍2.718 计算一个复利例子,设本金p,年利率为r,每月计算一次利息,月利息为r/12,则一年的本息一共: p(1+r/12)12=pq 当计算复利的时间间隔越来越小,根据上面极限公式,本金所乘的 ...
- Putty CentOS SSH 总是自动断开连接
/********************************************************************** * Putty CentOS SSH 总是自动断开连接 ...
- vue查缺补漏题
一.对于MVVM的理解? MVVM 是 Model-View-ViewModel 的缩写.Model代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑.View 代表UI 组件,它负责将数 ...
- hdoj-1068(二分图的最小点覆盖)
题目 1 问题转化: 求二分图最小点覆盖(覆盖所有的边) 2 问题的解决: 二分图最小点覆盖==其最大匹配数 3 证明: 链接 =#include <bits/stdc++.h> ...
- 陕西师范第七届K题----动态规划
ps: 自己的方法绝对是弱爆了 肯定存在更优的方法 O(n^3)复杂度 暴力求解的.. 链接:https://www.nowcoder.com/acm/contest/121/K来源:牛客网 柯怡最近 ...