Created
April 8, 2020 12:30
-
-
Save hitGovernor/c44c4d6ab0d03a6f10e2dbaa5abe0af5 to your computer and use it in GitHub Desktop.
A simple function to build a single campaign identifier (CID) string out of a collection of utm_ parameters.
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
/** | |
* 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