Custom JS variable evaluated multiple times per page


I seem to have a strange issue in which a Custom JS variable that I created is not only evaluated once on page load but also after every event?

I wanted to create a custom variable that allows me to identify if the current page is the first page in the session (by using a session storage item), the issue is that this script seems to be running not once per page but every time there is a new event (like timer or scroll), so it’s not working as I intended.

As soon as I delete the session storage item and scroll down the page the item is recreated again!

Is this how Custom JS variables are supposed to work? Running the same scripts over and over again for each event seems like a terrible use of compute resources, no?

Is there any way I can make sure a Custom JS variable is only evaluated once per page?

Thank you for your support!

Unfortunately, variable values get updated with each event that the visitor makes whether it is a scroll or click on the page. This is linked with the fact that it helps check if the variable values have not changed between events. This is mainly useful when updating values for built-in variables as their values are most prone to change between different events, for example, Page URL on the SPA pages on history change events. For now, there is no division between updating TM-generated variables and the built-in ones and that is why all variables fall under the same setting of being updated each time an event occurs.

If you would like to have more control over the creation of your session storage item, then I would encourage you to create a custom code tag instead of a custom JS variable. The custom code tag will allow you to prevent the script from running multiple times as you could simply use a trigger that would block such a behavior.

What is more, by creating a tag you will be allowed to play around with the multiplicity of a trigger that is supposed to launch the tag and this in turn will allow you to run a tag just once in a session:

However, if you would like to stay with creating a custom JS for that, then it would be best to add more “if” or “catch and try” statements which would somehow prevent the script from running multiple times.

@asinior Thank you for your reply, I have now replaced my previous logic with a tag that sets a cookie like you suggested, instead of checking for the first page in a session.

I was just surprised to see the Custom JS variable being evaluated for every event. I’m not 100% sure, but I have the impression that Google Tag Manager custom JS variables are evaluated only when they are being used in a given event, hence my confusion.

Speaking of which, it would be great if we could see the dependencies between tags, triggers and varibles, similar to those in Google Tag Manager.

Tag Manager tells us how many tags are attached to each trigger, but we don’t know which tags specifically. Not knowing exactly where each trigger and variable is currently being used is a real pain, especially as the number of objects in your container grows and there are several people working on the same container.

These are some good insights for a feature request material. I will create one and forward it to our developers.

1 Like


this will happen in Google Tag Manager in preview as well, as variable values are displayed for every event. This means, that if you log something to console in your variable code, you will see a lot of executions while in preview and significantly less when looking at a site using a live container without preview mode.

However, if a value does not change all the time and every execution will return the same value, just cache it in a JavaScript variable and return that value if present until the next page loads. That’s a good idea in every tag manager, I guess.



This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.