Skip to content

Instantly share code, notes, and snippets.

@hitGovernor
Created April 8, 2020 12:30
Embed
What would you like to do?
A simple function to build a single campaign identifier (CID) string out of a collection of utm_ parameters.
/**
* builds a single campaign identifier (CID) string out of a collection of utm_ parameters
* payload is required, but all properties of payload are optional
*/
function buildCIDfromUTM(payload) {
var delimiter = payload.delimiter || '_';
var cid = payload.cid || [
payload.medium,
payload.source,
payload.campaign,
payload.term,
payload.content
].join(delimiter);
// remove any TRAILING delimiters (will return '' if cid contains ONLY delimiters)
var patt = new RegExp(delimiter + "+$");
cid = cid.replace(patt, '');
return cid;
}
// EXAMPLE
var cid = buildCIDfromUTM({
delimiter: '~',
cid: '', // ?cid=
medium: 'ppc', // &utm_medium=
source: 'google', // &utm_source=
campaign: 'yougetthegist', // &utm_campaign=
term: 'hitgovernor', // &utm_term=
content: '' // &utm_content=
});
console.log(cid);
// result: "ppc~google~yougetthegist~hitgovernor"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment