Tracking | CCID Implementation Guide - Cookie
Introduction
This article presents an optional alteration to the standard CCID Implementation Guide.
For some implementations it may make more send to persist the ChannelMix Conversion ID (CCID) as a cookie. In that case, users should follow the guide linked above but ignore the Google Tag Manager (GTM) section, and use the following GTM instructions instead.
Through these will create the same required variables as the original guide but JavaScript snippets have been altered to use a cookie to persist CCID. Therefore, if you have already implemented the original guide you can simply make the updates below to the existing Variables and Tags.
Google Tag Manager Requirements:
Requirements are the same as the original guide with the exception of those variables and tags listed below. Use the snippets below to configure GTM for the CCID cookie-based implementation. Otherwise, following instructions provided in the standard CCID Implementation Guide.
- Variables
- ChannelMix Hash
- ChannelMix Session ID
- ChannelMix Conversion ID
- Custom HTML Tags (Scripts)
- CCID Reset Script
- CCID Hidden Input Support Script
[Variable] ChannelMix Hash
function(){
var cmx_hash = "";
var chars = "0123456789" +
"abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// enable this to enable special chars
// "-_.;!@%^*()=+~"
var length = 16;
for (var i = length; i > 0; --i)
cmx_hash += chars[Math.floor(Math.random() * chars.length)];
return cmx_hash;
}
[Variable] ChannelMix Session ID
function (){
var cookieName = 'cmx_sid';
var cookieValue = '{{cookie - cmx_sid}}';
if (cookieValue != 'undefined') {
return cookieValue;
} else {
var newSessionID = '{{Timestamp - Unix Epoch}}';
document.cookie = cookieName+"="+newSessionID;
return newSessionID;
}
}
[Variable] ChannelMix Conversion ID
function (){
var ccidCookieName = 'ccid';
var ccid = '{{cookie - ccid}}';
//if undefined, set the ccid cookie value
if (ccid != 'undefined') {
return ccid;
} else {
var newCCID = '{{Timestamp - Unix Epoch}}'+'-0'; //zero = 1st conversion of this session
document.cookie = ccidCookieName+"="+newCCID;
return newCCID;
}
}
[Custom HTML Tag] CCID Reset Script
<script>
(function(){
var ccid = {{cookie - ccid}};
var conversionCounter = ccid.split('-')[1];
conversionCounter = Number(conversionCounter);//as a number
conversionCounter = conversionCounter+1;//increment
conversionCounter = conversionCounter.toString();//back to a string
console.log("conversionCounter: "+conversionCounter);
//update the ccid
var newCCID = ccid.split('-')[0]+"-"+conversionCounter;
console.log("new ccid: "+newCCID);
document.cookie = "ccid="+newCCID;
})();
</script>
[Custom HTML Tag] CCID Hidden Input Support Script
<script>
(function(c,m,x){
var setCMXFormInput = function(cmx){
cmx = 'input#' + cmx + ',input[name='+cmx+']';
var cmx_input = document.querySelectorAll(cmx);
c['channelmix_conv_id'] = x;
if(cmx_input && cmx_input.length >= 1){
for(i in cmx_input)
cmx_input[i].value = x;
c[m].push({'event':'channelmix.setId','channelmix_conv_id': x });
console.info('ChannelMix Conversion ID Set!');
}else{
console.warn('ChannelMix Conversion ID NOT Set! - input not found');
}
document.body.dispatchEvent(new Event('ChannelMix.setId'));
};
setCMXFormInput(m);
})(window,'channelmix_conv_id','{{ChannelMix Conversion ID}}');
</script>
Conclusion
With the above updates implemented, you may return to the standard CCID Implementation Guide and continue with the next section or you may review the validation section to test updates made here.
The above GTM requirements may be available as an export (.json) file, upon request. Contact support@channelmix.com.