Skip to content

Instantly share code, notes, and snippets.

@herdianf
Created February 17, 2017 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herdianf/40663c410b8c090a31d16f5be5ae4f29 to your computer and use it in GitHub Desktop.
Save herdianf/40663c410b8c090a31d16f5be5ae4f29 to your computer and use it in GitHub Desktop.
function SuccessUID(UID)
{
var d = new Date();
d.setTime(d.getTime() + (365 * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = "UID=" + UID + "; " + expires + "; domain=.vizpro.sg; path=/";
cX.callQueue.push(['invoke', function () { // This is called when CX is ready, no need for callQueue.push after this.
if (UID != "" && UID != "null" && UID != "true" && UID != "false" && UID != "error" && UID != "undefined" && UID.length > 5)
{
cX.addExternalId({ id: UID, type: 'mdc' });
}
cX.setSiteId('1141834034534548467');
//for testing environment only.
//cX.sendPageViewEvent({ 'location': 'http://insert.test.url/here' });
//for production use below
cX.sendPageViewEvent();
}]);
}
(function($) {
function GenerateUID(SSOID) {
var cookies = ("cookie" in document && (document.cookie.length > 0 || (document.cookie = "test").indexOf.call(document.cookie, "test") > -1));
if (!cookies) {
return;
}
var UniversalID = "";
var LotameID;
var CxenseID;
//adding by script : additional parameter with new UID API backend
//Pass SSOID to lotame - Start
if (SSOID != "") {
_cc9615.add('tp', 'MCRP'); //'MCRP'
_cc9615.add('tpid', SSOID);
}
_cc9615.bcp();
//Pass SSOID to lotame - END
//end adding
UniversalID = getCookie("UID");
LotameID = typeof lotauds != 'undefined' ? lotauds.Profile.pid : '';
CxenseID = typeof cX.getUserId != 'undefined' ? cX.getUserId() : getCookie("cX_P");
if (typeof mcDataLayer != 'undefined') {
mcDataLayer.uid = UniversalID;
mcDataLayer.cxenseid = CxenseID;
mcDataLayer.lotameid = LotameID;
}
console.log( ['UniversalID=',UniversalID, 'SSOID=',SSOID, 'LotameID=',LotameID] )
if ((UniversalID != "" && SSOID == "") || (UniversalID == SSOID && SSOID != "")) {
//adding by script : additional parameter with new UID API backend
cX.callQueue.push(['invoke', function () { // This is called when CX is ready, no need for callQueue.push after this.
cX.setSiteId('1141834034534548467');
cX.addExternalId({ id: UniversalID, type: 'mdc' });
cX.sendPageViewEvent();
}]);
//end adding
} else {
//adding by script : additional parameter with new UID API backend
var uidscript = document.createElement('script');
uidscript.setAttribute('id', 'uidscript');
uidscript.type = "text/javascript";
uidscript.src = "https://uid.mediacorp.sg/api/Profiles/GetUID?LotameID="
+ LotameID + "&CxenseID=" + CxenseID + "&SSOID=" + SSOID + "&UniversalID=" + UniversalID;
uidscript.async = true;
document.body.appendChild(uidscript);
//end adding
/* remark by arif
var mapping = {
LotameID: LotameID,
CxenseID: CxenseID,
SSOID: SSOID,
UniversalID: UniversalID
}
$.support.cors = true;
$.ajax({
type: "POST",
data: JSON.stringify(mapping),
crossDomain: true,
url: "https://uid.mediacorp.sg/api/Profiles/GetUID",
contentType: "application/json",
success: function(result, status) {
setCookie("UID", result, 365);
if (typeof mcDataLayer != 'undefined') {
mcDataLayer.uid = result;
}
},
error: function(result, status) {}
});
*/
}
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
$(document).ready(function() {
try {
GenerateUID('');
} catch (e) {}
});
})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment