Last active
June 20, 2019 16:20
-
-
Save CarlosMecha/7dda02b8d20756823a8dbb1f657acb31 to your computer and use it in GitHub Desktop.
Adobe's Marketing Cloud Visitor ID
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
<html> | |
<head> | |
<title>Marketing Cloud Service</title> | |
<script> | |
window.organizationId = 'myOrganizationId'; | |
window.region = 3; | |
</script> | |
<script src="./mcvid.js"></script> | |
</head> | |
<body> | |
<script> | |
window.marketingCloudService.addEventListener('error', ev => console.log('Error: ' + ev.message)); | |
window.marketingCloudService.addEventListener('ready', () => { | |
// Load Segment SDK | |
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t,e){var n=document.createElement("script");n.type="text/javascript";n.async=!0;n.src="https://cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(n,a);analytics._loadOptions=e};analytics.SNIPPET_VERSION="4.1.0"; | |
analytics.load('myWriteKey'); | |
analytics.page('Home', {}, { "Adobe Analytics": { "marketingCloudVisitorId": window.mcvid } }); | |
}}(); | |
}); | |
</script> | |
</body> | |
</html> |
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
/** | |
* Retrives the visitorID from local storage or Adobe's Marketing Cloud service and exposes it to the window object as `window.mcvid`. | |
* | |
* To subscribe to events (errors, ready, etc), use `window.marketingCloudService.addEventListener(type, callback [, options])` | |
* | |
* Emitted events: | |
* - 'ready': When the visitor ID has been received and it's ready to be used. | |
* - 'error': When the Marketing Cloud service recieves an error. | |
* | |
* Until the 'ready' event gets emitted, the value of `window.mcvid` is `undefined`. | |
* | |
* Called as soon as the script is loaded. | |
*/ | |
(function(w) { | |
/** | |
* Service to interact with Adobe's Marketing Cloud. | |
* | |
* For more information about EventTarget, see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget | |
*/ | |
class MarketingCloudService extends EventTarget { | |
/** | |
* Creates the service with the provided Adobe's organization ID and region. | |
* @param {String} organizationId Adobe's organization ID. | |
* @param {int} region Datacenter region. | |
*/ | |
constructor(organizationId, region) { | |
super(); | |
this.organizationId = organizationId; | |
this.region = region; | |
} | |
/** | |
* Emits an error as Error Event. | |
* | |
* @param {String} message Error message. | |
*/ | |
_emitError(message) { | |
var error = new Event('error'); | |
error.message = message; | |
this.dispatchEvent(error); | |
} | |
/** | |
* Retrieves the visitor ID and adds it to `window.mcvid`. It also emits the event 'ready' | |
* when the operation is done, or 'error' if the operation has failed. | |
*/ | |
retrieveVisitorId() { | |
var self = this; | |
if (!self.organizationId || !self.region) { | |
self._emitError('Organization ID and region must be provided'); | |
return; | |
} | |
var previousVid = window.localStorage.getItem('mcvid'); | |
if (previousVid) { | |
w.mcvid = previousVid; | |
w.addEventListener('load', () => self.dispatchEvent(new Event('ready')), false); | |
return; | |
} | |
var request = new XMLHttpRequest(); | |
request.onload = function() { | |
if (request.status != 200) { | |
self._emitError(`Call to Marketing Cloud Service returned ${request.status}`); | |
return; | |
} | |
var response = request.response; | |
if (response.errors) { | |
self._emitError(`Marketing Cloud Service returned an error: ${response.errors[0].code} - ${response.errors[0].msg}`); | |
return; | |
} | |
if (!response.d_mid) { | |
self._emitError('Response does not contain visitor ID'); | |
return; | |
} | |
w.mcvid = response.d_mid; | |
self.dispatchEvent(new Event('ready')); | |
// Store in local storage. | |
window.localStorage.setItem('mcvid', w.mcvid); | |
}; | |
request.onerror = function (err) { | |
self._emitError(`Received error from Marketing Cloud service: ${err}`); | |
}; | |
request.open("GET", `http://dpm.demdex.net/id?d_ver=2&d_rtbd=json&d_orgid=${w.organizationId}@AdobeOrg&dcs_region=${w.region}`, true); | |
request.responseType = 'json'; | |
request.send(); | |
} | |
} | |
w.marketingCloudService = new MarketingCloudService(w.organizationId, w.region); | |
w.marketingCloudService.retrieveVisitorId(); | |
}(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment