Created
May 10, 2019 08:03
-
-
Save fergusKe/c6d9a8511519b2d084ab1e34391192aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
(function() { | |
var ecommerce = {{ecommerce}}; | |
var purchase = ecommerce['purchase']; | |
if(purchase != null && purchase != '') { | |
var cookie_id = Number(getCookie('td_id')); | |
var id = purchase['actionField']['id']; | |
console.log('cookie_id = ', cookie_id); | |
console.log('id = ', id); | |
if (cookie_id === id) return; | |
dataLayer.push({ | |
'ecommerce': { | |
'purchase': purchase | |
}, | |
'event': 'sendPurchase' | |
}); | |
setCookie('td_id', id, 90); | |
console.log('send purchase'); | |
} | |
function setCookie(cname, cvalue, exdays) { | |
var d = new Date(); | |
var exdays = exdays ? exdays : 7; // 預設 7 天 | |
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); | |
var expires = 'expires=' + d.toGMTString(); | |
var path = 'path=/'; | |
document.cookie = cname + '=' + cvalue + '; ' + expires + '; ' + path; | |
} | |
function getCookie(cname) { | |
var name = cname + '='; | |
var ca = document.cookie.split(';'); | |
for(var i=0; i<ca.length; i++) { | |
var c = ca[i].trim(); | |
if (c.indexOf(name)==0) return c.substring(name.length,c.length); | |
} | |
return ''; | |
} | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment