Global JavaScript & Global Tracking JS
In short
With Global JavaScript you add your own JavaScript once for your whole account. It runs on every page where the Varify snippet is installed, and always before any experiment code. Use it for shared helpers, polyfills or setup logic that your experiments rely on.
Global Tracking JS works the same way, with one difference: it respects your Tracking Activation Script and only runs once tracking has been activated for the visitor. Use it for analytics and tracking integrations.
Both are intended for users with programming experience.
Where to find Global JavaScript
- In your Varify account, open Team Settings.
- Select Global JavaScript in the settings menu.
- Enter your code in the JavaScript field (Global JS) or in the Tracking JavaScript field (Global Tracking JS).
- Click Save Global JS, or the save button of the Global Tracking JS section.
The badge next to each section shows whether code has already been added (Not set means the field is empty). Both editors format your code automatically when you save. You can also format it manually with Format code or with the shortcut ⇧⌥F.
Global JS and Global Tracking JS at a glance
| Global JS | Global Tracking JS | |
|---|---|---|
| Runs on | Every page where the Varify snippet is installed | Every page where the Varify snippet is installed |
| Runs when | Before any experiment code | Once tracking has been activated for the visitor |
| Depends on the Tracking Activation Script | No | Yes |
| Typical use | Shared helpers, polyfills, setup logic | Analytics and tracking integrations |
How Global JS works
- One place for your whole account: You add the code once in the Team Settings. You do not have to copy it into each experiment or variation.
- Runs on every page with the snippet: The code is not tied to an experiment. It runs on every page where the Varify snippet is installed.
- Executed first: Global JS always runs before any experiment code. Code you add to an experiment or a variation (see Experiment JavaScript & Experiment CSS and Add JavaScript) can therefore build on what Global JS has already set up.
Because the code runs on every page, keep it short and test it carefully. Put code that only belongs to one experiment into the experiment or variation instead.
Example: provide a shared helper
Define a helper once in Global JS and use it in the code of any experiment.
// Global JS: define the helper once
window.myHelpers = window.myHelpers || {};
window.myHelpers.formatPrice = (value) =>
new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(value);
// Variation JavaScript: use the helper
document.querySelector('.price').textContent = window.myHelpers.formatPrice(49);
Example: add a jQuery library
If your experiments use jQuery but your website does not load it, you can add the library once in Global JS instead of in every experiment. The code below only loads jQuery if the page does not have it yet.
// Global JS: load jQuery once, if the page does not provide it
if (window.jQuery) {
window.jQueryReady = Promise.resolve(window.jQuery);
} else {
window.jQueryReady = new Promise((resolve) => {
const script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.7.1.min.js';
script.onload = () => resolve(window.jQuery);
document.head.appendChild(script);
});
}
The library is loaded asynchronously. Because of this, wait for window.jQueryReady in your experiment code before you use jQuery:
// Variation JavaScript: use jQuery as soon as it is available
window.jQueryReady.then(($) => {
$('.price').addClass('highlight');
});
Adjust the version in the URL to the one you need. If your website uses a Content Security Policy, it may block scripts from external domains. In that case, see Content Security Policy (CSP) error in the console.
How Global Tracking JS works
Global Tracking JS runs on the same pages as Global JS, but it respects your Tracking Activation Script. The code only runs once tracking has been activated for the visitor. It does not run for visitors who have not been activated for tracking, for example before they have given consent.
You configure the Tracking Activation Script in the Delivery settings. The info box in the Global Tracking JS section links to them directly.
Example: send a custom event to the dataLayer
// Global Tracking JS: runs only once tracking is activated
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'varify_tracking_ready',
});
Which one should I use?
- Use Global JS if the code has to be available on every page, independent of tracking. This is the case for helpers, polyfills, libraries such as jQuery and setup logic.
- Use Global Tracking JS if the code belongs to analytics or tracking and may only run once tracking is active.
- Use Experiment JavaScript if the code is only needed for the variations of a single experiment.