1. Home
  2. Modules
  3. Standard Experiences
  4. Code Snippets for Ensuring UTM Values Persist for PathFactory CTAs on Your Website

Code Snippets for Ensuring UTM Values Persist for PathFactory CTAs on Your Website

The following script can be leveraged to ensure that CTAs that include PathFactory links on your website are retaining the UTM Values that are being utilized on your website campaigns. This allows customers to have more consistent tracking of these query string parameters that are driving traffic to PathFactory experiences. 

To see more examples of code snippets, read Frequently Used External Code Snippets to Personalize Your PathFactory Experiences.

Where to place the script?

This script can be added directly to your website pages or via GTM. The code will need to appear within the Head Tag.

Underlined codes are the domains that will need to be adjusted to contain your PathFactory sharing domain.

Bolded codes are the settings that you need adjust according to the query parameters you want to persist.

Code snippet

<script>

(function() {

  var domainsToDecorate = [

          ‘PFsubdomain.clientname.com

      ],

      queryParams = [

          ‘utm_medium’, //add or remove query parameters you want to transfer

          ‘utm_source’,

          ‘utm_campaign’,

          ‘utm_content’

      ]

  // do not edit anything below this line

  var links = document.querySelectorAll(‘a’);

// check if links contain domain from the domainsToDecorate array and then decorates

  for (var linkIndex = 0; linkIndex < links.length; linkIndex++) {

      for (var domainIndex = 0; domainIndex < domainsToDecorate.length; domainIndex++) { if (links[linkIndex].href.indexOf(domainsToDecorate[domainIndex]) > -1) {

              links[linkIndex].href = decorateUrl(links[linkIndex].href);

          }

      }

  }

// decorates the URL with query params

  function decorateUrl(urlToDecorate) {

      urlToDecorate = (urlToDecorate.indexOf(‘?’) === -1) ? urlToDecorate + ‘?’ : urlToDecorate + ‘&’;

      var collectedQueryParams = [];

      for (var queryIndex = 0; queryIndex < queryParams.length; queryIndex++) {

          if (getQueryParam(queryParams[queryIndex])) {

              collectedQueryParams.push(queryParams[queryIndex] + ‘=’ + getQueryParam(queryParams[queryIndex]))

          }

      }

      return urlToDecorate + collectedQueryParams.join(‘&’);

  }

  // borrowed from https://stackoverflow.com/questions/831030/

  // a function that retrieves the value of a query parameter

  function getQueryParam(name) {

      if (name = (new RegExp(‘[?&]’ + encodeURIComponent(name) + ‘=([^&]*)’)).exec(window.location.search))

          return decodeURIComponent(name[1]);

  }

})();

</script>

Updated on July 24, 2023

Was this article helpful?

Need Support?
Can't find the answer you're looking for? Don't worry we're here to help!
CONTACT SUPPORT