1

I have created the following custom javascript variable in Google Tag Manager:

function() {
  return {{Click Text}} || {{Click Element}};
}

which uses the built in variables Click Text and Click Element. I am using this variable as the label of a tag, which is triggered by any click.

This works perfectly well when the tags are triggered from localhost and the labels arrive correctly to Google Analytics (I'm using universal analytics). However, for my production website the label always shows up as (not set) in Google Analytics. What causes this discrepancy?

5
  • New version published? Commented Mar 22, 2022 at 19:22
  • 1
    @EikePierstorff, yes, currently showing "Workspace changes: 0" when I do the preview and the (not set) only started appearing after I adjusted the label from what it previously was to the new custom variable. Commented Mar 22, 2022 at 19:53
  • 1
    Just making sure :-) Can you open your network tools in your browser and see what the actual request looks like? Commented Mar 22, 2022 at 20:29
  • @EikePierstorff, thanks for the tip about the network tab, that hadn't occured to me. I now realise the issue is not with preview, it is with localhost vs my production website. When I look at the event on localhost the "el" param is there but not on my website. Any idea why? Commented Mar 22, 2022 at 21:25
  • Ah, it's caused by the content security policy. I hadn't considered this because normally when something is blocked by the CSP it shows up in the console. But in this case it doesn't. Commented Mar 22, 2022 at 22:02

1 Answer 1

5

The reason this was not working for me was because I had a content security policy enabled on my production website, and for custom javascript variables to work with a CSP they need script-src: 'unsafe-eval' in the policy. This was also hard to debug because there is no error raised in the console about the CSP blocking the script.

To resolve this you can either add the above to the policy or alternatively use custom variable templates to run javascript in a way that doesn't require unsafe-eval. Unfortunately, this doesn't work for my example using {{Click Element}}, since it is not allowed in the template, but it works well for other situations where you might want to use a custom javascript variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.