Skip to content

Instantly share code, notes, and snippets.

@hitGovernor
Created April 8, 2020 12:30
Show Gist options
  • Save hitGovernor/c44c4d6ab0d03a6f10e2dbaa5abe0af5 to your computer and use it in GitHub Desktop.
Save hitGovernor/c44c4d6ab0d03a6f10e2dbaa5abe0af5 to your computer and use it in GitHub Desktop.

Revisions

  1. hitGovernor created this gist Apr 8, 2020.
    34 changes: 34 additions & 0 deletions buildCIDfromUTM.js
    Original 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"