Skip to content

Instantly share code, notes, and snippets.

@satour
Created May 12, 2020 07:13
Show Gist options
  • Save satour/43645debe65c38187f9c36564b2c440a to your computer and use it in GitHub Desktop.
Save satour/43645debe65c38187f9c36564b2c440a to your computer and use it in GitHub Desktop.
<script type="text/javascript">
var tdSetting = {
host: 'in.treasuredata.com',
writeKey: 'PLACEHOLDER',
database: 'PLACEHOLDER',
startInSignedMode: true,
_sdk: '2.2.0',
_sdkMethods: ["blockEvents","unblockEvents","setSignedMode","setAnonymousMode","resetUUID","fetchServerCookie","addRecord","fetchGlobalID","fetchUserSegments","set","trackEvent","trackPageview","trackClicks","ready"]
}
var tdDomainSpecificSettings = {
'www.example.com': {
useServerSideCookie: true,
sscDomain: 'example.com',
sscServer: 'ssc.example.com',
},
'example.jp': {
useServerSideCookie: true,
sscDomain: 'example.jp',
sscServer: 'ssc.example.jp'
},
'default': {}
}
var td_table = 'PLACEHOLDER';
var hostName = location.hostname;
var index = tdDomainSpecificSettings.hasOwnProperty(hostName) ? hostName : 'default';
for (var i in tdDomainSpecificSettings[index]) {
tdSetting[i] = tdDomainSpecificSettings[index][i]
}
!function(t,e){if(void 0===e[t]){e[t]=function(){e[t].clients.push(this),this._init=[Array.prototype.slice.call(arguments)]},e[t].clients=[];for(var r=function(t){return function(){return this["_"+t]=this["_"+t]||[],this["_"+t].push(Array.prototype.slice.call(arguments)),this}},s=tdSetting._sdkMethods,n=0;n<s.length;n++){var c=s[n];e[t].prototype[c]=r(c)}var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src=("https:"===document.location.protocol?"https:":"http:")+"//cdn.treasuredata.com/sdk/" + tdSetting._sdk + "/td.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(o,a)}}("Treasure",this);
var td = new Treasure(tdSetting);
if (tdSetting.startInSignedMode) {
td.set('$global', {
'td_global_id': 'td_global_id',
'td_ssc_id': {{'_td_ssc_id'}}
});
var appendPixel = function() {
td.fetchGlobalID(function(td_global_id) {
var img = document.createElement("img");
img.src = "https://pixel.example.com/idsync/?td_global_id=" + td_global_id;
img.width = 1;
img.height = 1;
img.style.display = 'none';
document.body.appendChild(img);
})
}
switch (index) {
case "default":
td.trackPageview(td_table, appendPixel);
break;
default:
if (tdSetting.useServerSideCookie){
if ( location.protocol == 'https:'){
var fetchServerCookieSuccessCallback = function(result) {
td.set("$global", { td_ssc_id: result });
td.trackClicks({ tableName: 'click' });
td.trackPageview(td_table, appendPixel);
}
td.fetchServerCookie(fetchServerCookieSuccessCallback, function(){td.trackPageview(td_table, appendPixel)});
}else if(location.protocol == 'http:'){
td.trackPageview(td_table, appendPixel);
}else{
console.error('td-js-sdk does not support the protocol: ', location.protocol)
}
}else{
td.trackPageview(td_table, appendPixel); //useServerSideCookie: false
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment