# Custom JS variable evaluated multiple times per page

**URL:** <https://community.piwik.pro/t/custom-js-variable-evaluated-multiple-times-per-page/3927>\
**Category:** Data collection and tags\
**Created:** [April 24, 2024, 9:24am UTC](https://community.piwik.pro/t/custom-js-variable-evaluated-multiple-times-per-page/3927 "2024-04-24T09:24:04Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![mbaersch](https://dub1.discourse-cdn.com/flex005/user_avatar/community.piwik.pro/mbaersch/32/35_2.png) [@mbaersch](https://community.piwik.pro/u/mbaersch)\
**Post date:** [April 25, 2024, 5:50pm UTC](https://community.piwik.pro/t/custom-js-variable-evaluated-multiple-times-per-page/3927/6 "2024-04-25T17:50:36Z")

</div>

Hi,

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.

best,  
Markus

---

_[View the full topic](https://community.piwik.pro/t/custom-js-variable-evaluated-multiple-times-per-page/3927)._
