頁面加載時需要埋點但未實現,斷點發現sensors未加載成功,故封裝方法不停判斷,直到頁面加載好這個方法再進行埋點
// 埋點方法
doTrack(name, data) {
let time = ''
let fn = () => {
if (!window.sensors || !window.sensors.track) {// 如果沒有這個方法就每10秒調用一次
clearTimeout(time)
time = window.setTimeout(fn, 10)
} else {
window.sensors.track(name, data)
}
}
time = window.setTimeout(fn, 10)
},
使用
this.doTrack('NavigationPagePV', {
UrlPlatform: 'PC',
NavigationPage: '上屆獲獎作品頁',
Channel: Channel,
})
sdk封裝
import sensors from'sa-sdk-javascript';
sensors.init({
server_url: !/kuaikanmanhua.com/.test(location.href) ? 'https://sa.kkmh.com/sa?project=kuaikan_test':'https://sa.kkmh.com/sa?project=default',//?project=default
show_log:!/kuaikanmanhua.com/.test(location.href) ? true : false, //線上環境禁止輸出埋點信息
heatmap: {
//是否開啓點擊圖,默認 default 表示開啓,自動採集 $WebClick 事件,可以設置 'not_collect' 表示關閉
clickmap:'default',
//是否開啓觸達注意力圖,默認 default 表示開啓,自動採集 $WebStay 事件,可以設置 'not_collect' 表示關閉
scroll_notice_map:'not_collect',
//設置多少毫秒後開始渲染點擊圖,因爲剛打開頁面時候頁面有些元素還沒加載
loadTimeout: 3000,
//show_log:false
//用戶點擊(a,button,input)這些元素時會觸發這個函數,讓你來判斷是否要採集當前這個元素,返回真表示採集,返回假表示不採集。
//不設置這個函數,默認是採集 a button input 這些標籤。
// collect_element: function(element_target){
// // 如果這個元素有屬性sensors-disable=true時候,不採集
// if(element_target.getAttribute('sensors-disable') === 'true'){
// return false;
// }else{
// return true;
// }
// },
},
debug_mode:!/kuaikanmanhua.com/.test(location.href) ? true : false, //線上環境是圖片上報信息,測試環境使用ajax上傳數據
});
var sdkTime;
function initTime (){
window.clearTimeout(sdkTime);
//測試環境禁用 alert
if( !/kuaikanmanhua.com/.test(location.href) ){
window.alert=(...ary)=>{return ary};
}
let user_ids;
if(window.user_ids != 'undefined' && window.user_ids != '0'){
user_ids = window.user_ids;
}
if(user_ids){
sensors.login(user_ids);
}
window.sensors = sensors;
let distinct_id = sensors.store.getDistinctId().replace(/\s*/g,"");
if(!distinct_id){ // distinct_id==''的情況下 說明是退出登錄狀態
sensors.logout(true);
}
sensors.quick('autoTrack', {
platForm:'pc'
});
}
sdkTime = window.setTimeout(initTime,100);
// window.addEventListener
// sensors.login(user_id);
// sensors.quick('autoTrack');