Skip to content

Instantly share code, notes, and snippets.

@fergusKe
Created May 10, 2019 08:03
Show Gist options
  • Save fergusKe/c6d9a8511519b2d084ab1e34391192aa to your computer and use it in GitHub Desktop.
Save fergusKe/c6d9a8511519b2d084ab1e34391192aa to your computer and use it in GitHub Desktop.
<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