Created
April 8, 2020 12:30
-
-
Save hitGovernor/c44c4d6ab0d03a6f10e2dbaa5abe0af5 to your computer and use it in GitHub Desktop.
Revisions
-
hitGovernor created this gist
Apr 8, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ /** * 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"