Brando
Brando

Reputation: 88

GTM URL Decoding

I'm a bit new to the GTM world and the current JavaScript existed prior to me taking over the project. My problem is that campaign codes are not decoding like they should (e.g. qwer%2aqwer). I'm trying to add the encodingURL to try and clean it up, but I'm having issues getting it to work.

function() {
    try {
        if ({{redirectParams}}){
            var redirectParams = {{redirectParams}},
                url = {{Page URL}},
                utm_campaign = redirectParams.utm_campaign;

            if (url.indexOf('utm_campaign') === -1 && url.indexOf('utm_source') === -1 && url.indexOf('utm_medium') === -1 && url.indexOf('utm_content') === -1 && url.indexOf('utm_term') === -1 && url.indexOf('gclid') === -1 && url.indexOf('dclid') === -1) {
                return utm_campaign
            }

I would think I should be able to add the decodeURIcomponent to the "return utm_campaign" syntax, but it's not working.

I would think I could just add it to the "return utm_camapin" syntax, but that's not the case. (e.g. return decodeURIcomponent('utm_campaign)

Upvotes: 1

Views: 1915

Answers (1)

Robert Miller
Robert Miller

Reputation: 121

Double check the code you typed in the question vs. what you have in GTM. There are some issues in the provided code that would break the js function from returning a value in GTM.

Here is some code that should work in GTM.

function() {
  try {
    if ({{redirectParams}}) {
      var redirectParams = {{redirectParams}},
        url = {{Page URL}},
        utm_campaign = redirectParams.utm_campaign;

      if (url.indexOf('utm_campaign') === -1 && url.indexOf('utm_source') === -1 && url.indexOf('utm_medium') === -1 && url.indexOf(
          'utm_content') === -1 && url.indexOf('utm_term') === -1 && url.indexOf('gclid') === -1 && url.indexOf('dclid') ===
        -1) {
        return decodeURICompnent(utm_campaign);
      }
    }
  } catch (e) {}
}

Upvotes: 1

Related Questions