javascript BlueKai CoreTag - 每次访问花费的时间,每个会话的页数,一天中的时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript BlueKai CoreTag - 每次访问花费的时间,每个会话的页数,一天中的时间相关的知识,希望对你有一定的参考价值。
/*
Author : roshan.gonsalkorale@oracle.com
Notes:
- window._bk.functions.time_spent() & window._bk.functions.pages_per_visit() use session cookies stored on the top-level domain, e.g. ".mydomain.com" or ".mydomain.co.uk" to calculate values
*/
window._bk = window._bk || {};
window._bk.data = window._bk.data || {};
window._bk.functions = window._bk.functions || {};
// DOMAIN GRABBER
window._bk.functions.domainGrabber = function(){
// Replace www.
var domain = document.domain.replace('www.','');
//Detect .xx.xx vs .xxx
var reverse_domain = domain.split('.').reverse();
if (reverse_domain[0].length === 2) {
var domain_length = 2;
var tld = "." + reverse_domain[1] + "." + reverse_domain[0];
} else if (reverse_domain[0].length === 3){
var domain_length = 1;
var tld = "." + reverse_domain[0];
}
// Create final domain
if(domain_length === 2){
domain = "." + reverse_domain[2] + tld;
} else if (domain_length === 1){
domain = "." + reverse_domain[1] + tld;
}
return domain;
}
// Cookie Setter
window._bk.functions.cookie_setter = function(cookieName,cookieValue){
//session cookie
document.cookie = cookieName + "=" + cookieValue + ";path=/;domain=" + window._bk.functions.domainGrabber() + ";expires=";
};
// Cookie Reader
window._bk.functions.cookie_reader = function(cookieName){
// Get name followed by anything except a semicolon
var cookiestring=RegExp(""+cookieName+"[^;]+").exec(document.cookie);
// Return everything after the equal sign, or an empty string if the cookie name not found
return decodeURIComponent(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./,"") : "");
};
// PAGES PER VISIT (set args to do different things)
window._bk.functions.pages_per_visit = function(){
if(!window._bk.functions.cookie_reader("_bk_ppv")){
window._bk.data.ppv = 1;
} else if(typeof window._bk.data.ppv === "undefined"){
window._bk.data.ppv = parseInt(window._bk.functions.cookie_reader("_bk_ppv")) + 1;
}
window._bk.functions.cookie_setter("_bk_ppv",window._bk.data.ppv);
//Set bandings
var bandings = [[1],[2,4],[5,10],[11,1000]];
for (var i = 0; i < bandings.length; i++) {
// Single Length banding
if(bandings[i].length === 1){
if(window._bk.data.ppv === bandings[i][0]){
window._bk.data.ppv_banding = bandings[i][0];
}
} else if(window._bk.data.ppv >= bandings[i][0] && window._bk.data.ppv <= bandings[i][1]) {
window._bk.data.ppv_banding = bandings[i][0] + "-" + bandings[i][1];
}
}
return {
"ppv":window._bk.data.ppv,
"ppv_band":window._bk.data.ppv_banding
}
};
// TIME OF DAY (set args to do different things)
window._bk.functions.time_of_day = function(){
_bk.data.hourOfDay = new Date().getHours() + "00";
return _bk.data.hourOfDay;
};
// TIME SPENT (set args to do different things)
window._bk.functions.time_spent = function(){
var currentTimestamp = parseInt(new Date().valueOf()/1000);
if(!window._bk.functions.cookie_reader("_bk_vst")){
window._bk.functions.cookie_setter("_bk_vst",currentTimestamp);
window._bk.data.session_time_s = "n/a - first page view/event";
window._bk.data.session_time_band = "n/a - first page view/event";
} else {
// time bands
var session_time_bands = [[0,10],[11,30],[60,180],[181,600],[601,1800],[1801,100000]]
// Calculate Session Time
window._bk.data.session_time_s = currentTimestamp - parseInt(window._bk.functions.cookie_reader("_bk_vst"));
// Reset to "n/a - first page view/event" on first PV
if(window._bk.data.session_time_s === 0){window._bk.data.session_time_s = "n/a - first page view/event"};
for (var i = 0; i < session_time_bands.length; i++) {
if(window._bk.data.session_time_s >= session_time_bands[i][0] && window._bk.data.session_time_s <= session_time_bands[i][1]){
window._bk.data.session_time_band = session_time_bands[i][0] + "-" + session_time_bands[i][1] + " seconds";
}
}
}
return {
"ts":window._bk.data.session_time_s,
"ts_band":window._bk.data.session_time_band
}
};
// Ensure you use these functions on every call or they may stop working
window._bk.functions.pages_per_visit()
window._bk.functions.time_spent();
// Declare Phints
bk_addPageCtx('page_views_per_visit_seconds', window._bk.functions.pages_per_visit().ppv);
bk_addPageCtx('pages_views_per_visit_band', window._bk.functions.pages_per_visit().ppv_band);
bk_addPageCtx('time_spent_per_visit_seconds', window._bk.functions.time_spent().ts);
bk_addPageCtx('time_spent_per_visit_band', window._bk.functions.time_spent().ts_band);
bk_addPageCtx('hour_of_day', window._bk.functions.time_of_day());
以上是关于javascript BlueKai CoreTag - 每次访问花费的时间,每个会话的页数,一天中的时间的主要内容,如果未能解决你的问题,请参考以下文章
javascript BlueKai CoreTag - 元数据
javascript BlueKai:VWO代码:TEMPLATE