Skip to content

Instantly share code, notes, and snippets.

@fergusKe
Last active November 9, 2018 10:06
Show Gist options
  • Save fergusKe/90a3c80bab25a0b425c398ed4bc07ae9 to your computer and use it in GitHub Desktop.
Save fergusKe/90a3c80bab25a0b425c398ed4bc07ae9 to your computer and use it in GitHub Desktop.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
(function($) {
var dataLayer = window.dataLayer || [];
var eventString = 'cusevent';
var eventCategoryString = '2018淡季早鳥優惠';
var eventInfo = [
['.pc-a-bt td:nth-child(1)', '服務按鈕點擊', '預約租車'],
['.pc-a-bt td:nth-child(2)', '服務按鈕點擊', '加入會員'],
['.pc-b-bt td:nth-child(1)', '下方導航列點擊', '回和運租車首頁'],
['.pc-b-bt td:nth-child(2)', '下方導航列點擊', '熱門優惠資訊'],
['.rbt a', '右側浮動banner點擊', '網路預約GO']
];
for ( var k2 in eventInfo ){
normalEvent(eventInfo[k2][0], eventInfo[k2][1], eventInfo[k2][2]);
}
function normalEvent(thisTarget, thisAction, thisLabel) {
var label = '';
var ratio = 0;
$(thisTarget).click(function() {
ratio = scrollRatio(25);
if (isNaN(ratio)) return; // 過濾掉錯誤的資訊
if (thisTarget.indexOf('.rbt') > -1) {
label = '網路預訂' + ' - ' + ratio + '%';
pushDatalayer('右側浮動banner點擊', label);
}
pushDatalayer(thisAction, thisLabel);
});
}
function pushDatalayer(thisAction, thisLabel) {
dataLayer.push({
event: eventString,
eventCategory: eventCategoryString,
eventAction: thisAction,
eventLabel: thisLabel
});
}
function scrollRatio(percentage){
var domHeight = Math.max(
document.body.scrollHeight,
document.body.offsetHeight,
document.documentElement.clientHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight );
return parseInt(window.scrollY/(domHeight-window.innerHeight)*100/percentage)*percentage;
}
})(jQuery)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment