ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
1. send: "事件的hit"這個動作,用sent指令
1. event: "hit"的種類記錄
使用 GTM 做為 GA 接收資料的 trigger
目的: 記錄使用者從何種途徑到達指定的頁面,可以區分不同網頁廣告來源的點擊次數
在超連結中,新增 class="js-monitor-params"
的屬性,這個屬性經由
以下 js 把參數 jobsource到下個網頁的超連結上如:
html
裡的 body
最後面引入var MonitorAdParm = {};
var MonitorClass = MonitorClass || {};
MonitorAdParm = (function () {
var getUrlParm;
var addUrlParm;
getUrlParm = function (queryKey) {
var str = '';
var result = '';
var keyWord = queryKey;
var url = '';
var urlArr = [];
url = document.location.search.toString();
if( url.indexOf('#') !== -1 ){
url = url.substring(0, url.indexOf('#'));
}
if (url.indexOf('?') !== -1) {
urlArr = url.split('?')[1].split('&');
for (i in urlArr) {
str = urlArr[i].split('=')[0];
if (str === keyWord) {
result = urlArr[i];
}
}
}
return result;
};
addUrlParm = function (urlParm, linkClass) {
var linkDoms = document.querySelectorAll(linkClass);
var linkDomSize = linkDoms.length;
var hasSharpMark = function(url){
var has = true;
if(url.indexOf('#') === -1){
has = false;
}
return has;
}
var hasQuestionMark = function (url) {
var has = true;
if (url.indexOf('?') === -1) {
has = false;
}
return has;
}
if (linkDomSize < 0 || !urlParm) return false;
for (var idx in linkDoms) {
if (linkDoms.hasOwnProperty(idx)) {
if (idx === 'length') {
return false;
}
var changeUrl = linkDoms[idx].href;
var sharpMark = '';
if( hasSharpMark(changeUrl) ){
var sharpIdx = changeUrl.indexOf('#');
sharpMark = changeUrl.substring(sharpIdx,changeUrl.length);
changeUrl = changeUrl.substring(0,sharpIdx);
}
var connection = '&';
if (!hasQuestionMark(changeUrl)) {
connection = '?';
}
if(changeUrl.indexOf(urlParm) !== -1) {
changeUrl = changeUrl + sharpMark;
linkDoms[idx].href = changeUrl;
}else{
changeUrl = changeUrl + connection + urlParm + sharpMark;
linkDoms[idx].href = changeUrl;
}
console.log(linkDoms[idx].href);
}
}
};
return {
getUrlParm: getUrlParm,
addUrlParm: addUrlParm
};
}());
MonitorClass = {
count: 0,
interval: {},
loopLimit: 0,
targetClass: '.js-monitor-params',
onFindClass: function () {},
onUnfindClass: function () {},
init: function (options) {
this.onFindClass = options.onFindClass;
this.onUnfindClass = options.onUnfindClass;
this.loopLimit = options.loopLimit;
this.targetClass = options.targetClass;
var linkDoms = document.querySelectorAll(this.targetClass);
if (linkDoms.length > 0) {
options.onFindClass();
} else {
this.loop();
}
},
loop: function () {
var limit = this.loopLimit;
var onFindClass = this.onFindClass;
var onUnfindClass = this.onUnfindClass;
var count = this.count;
MonitorClass.interval = setInterval(function () {
var linkDoms = document.querySelectorAll(this.targetClass);
var linkDomSize = linkDoms.length;
count = count + 1;
if (linkDomSize > 0) {
clearInterval(MonitorClass.interval);
onFindClass();
}
if (count === limit) {
clearInterval(MonitorClass.interval);
}
if (count === limit && linkDomSize === 0) {
onUnfindClass();
}
}, 1000);
},
};
$(function () {
MonitorClass.init({
targetClass: '.js-monitor-params',
loopLimit: 3,
onFindClass: function () {
var parm = MonitorAdParm.getUrlParm('jobsource');
MonitorAdParm.addUrlParm(parm, '.js-monitor-params');
},
onUnfindClass: function () {
console.log('Can not find class name: js-monitor-params');
},
});
});