Running Experiments on Single Page Apps
How experiences run on single page apps, and how to trigger them by hand when you need to
| Author: | George F. Crewe |
THIS ARTICLE WILL HELP YOU:
- Know what the script does on its own
- Remove old polling code
- Know when to trigger by hand
- Use a location trigger
- Run one experience by hand
- Run the script again
- Check goals again
- Find answers to common questions
What the Script Does on Its Own
Single page apps (SPAs), built with frameworks like React, Vue, Angular or Next.js, change the URL and redraw parts of the page without loading a new page. Convert's tracking script has SPA optimizations built in, and they're on by default in every Web project. You don't need to add polling code.
To run an experience on a SPA:
- Install the tracking code once on your app. See How to Install the Main Tag / Convert Tracking Code / JavaScript.
- Create the experience with a normal location, for example a Page URL condition for the route you want to test.
When a visitor moves to another route, the script:
- picks up the new URL the next time the page changes, and checks your experiences' locations, audiences and goals against it;
- removes the elements and styles that variations added on the previous route, then applies the variations that match the new route;
- applies a change again if your app overwrites the element the change was made on.
SPA Optimizations explains each of these and shows where to turn them off.
📒Note: The script removes what variations inserted and the styles they added. It doesn't undo edits to elements that stay on the page, or anything your own JavaScript adds. See Single Page Application (SPA) Troubleshooting Guide if a change stays after the visitor moves on.
Remove Old Polling Code
Earlier versions of this article told you to push run yourself whenever your app changed route. With today's script you don't need to. If you added any of the following, remove it:
- a
_conv_q.pushcall forrun, for example["run","true"]; - code that wraps
history.pushStateorhistory.replaceState; - a
setIntervalthat compares the URL; popstateorhashchangelisteners that callrun.
Look in Configuration > Global Javascript, in your site's code and in your tag manager.
The script already runs again by itself when the URL changes, so these calls repeat work it does. Global Javascript also runs again each time the script runs, so a snippet that wraps history.pushState without checking whether it already did can wrap it again each time.
📒Note: If your project shows Use Legacy Script under Configuration > Environments & Tracking Code, it still runs the legacy tracking script, which is deprecated. Follow Migrate from the Legacy Tracking Script to the New Tracking Script (Step-by-Step) first. Location triggers aren't available on the legacy script.
When to Trigger by Hand
Most experiences need none of this. Trigger by hand when:
- The experience should start at a moment, not on a URL. A visitor opens a modal, reaches step 3 of a flow, clicks a button, or data finishes loading. Use a location trigger.
- The Triggers menu doesn't offer the trigger you need. You can start one experience from your own code instead. See Run one experience by hand.
- SPA optimizations are turned off. The checkbox Disable Single Page App Optimizations is checked. See Run the script again.
- A goal depends on something that changes inside the page. See Check goals again.
Use a Location Trigger
A location's trigger decides when an experience starts once the location's conditions match. You set it on the location's Advanced tab.
- Open your project and click the Locations tab. Click New Location, or click an existing location to edit it.
- In the New Location window, click the Advanced tab. Existing locations open on it.

- Under Triggers, choose how the location activates:
- Upon Run: the location activates as soon as the script runs and its conditions match. With SPA optimizations on, the script runs again after each route change, so this works across routes. It's the default.
- DOM Element: the location activates when a visitor interacts with an element. Enter a Selector, or click Use Live Select to pick the element on your page, and choose one or more Events: Click, Change, Hover or In View. If the element isn't on the page yet, the script waits for it.
- Manually: the location activates when your code triggers it. The window shows the code to embed.
- When a callback is called: the location activates when your JavaScript calls
activate().
- Add the location's conditions, then click Add Location (or Save Location when you edit one).
📒Note: Which triggers you can choose depends on your plan and tracking script version. See What are Locations? for what each one is for.
Trigger a Location Manually
The window for the Manually trigger says Embed the code below into your page at the time you want the Location to activate. Call it from your app when the moment comes, for example when a modal opens or a step of a flow appears:
window._conv_q = window._conv_q || [];
window._conv_q.push({
what: "triggerLocation",
params: {
locationId: "123456" // replace with your location's ID
}
});
Find the location's ID in the ID column of the project's Locations tab.
- Call it after the Convert script has loaded and run once, from an event in your app, not at page start.
- The location's conditions still apply. If they don't match when you call it, nothing starts.
- URL conditions are compared with the URL the script read the last time it ran, and the script reads the new URL after the page changes. Don't call
triggerLocationin the same step as the navigation. Call it once the new view has rendered. - If the experience should start whenever a route loads, use When a callback is called instead. The script runs that callback again after each route change, so you don't have to call it yourself. Keep Manually for moments inside a route, such as a modal or a step.
- A location activates once each time the script runs. A second call before the script runs again does nothing. With SPA optimizations on, the script runs again after a route change and removes what variations added, so call
triggerLocationagain on the routes where the experience should show. - Add
triggerIntegrations: falsetoparamsif you don't want Convert to send the experience view to your analytics integrations.
Trigger a Location With a Callback
The code you enter for When a callback is called is the body of function (activate, options):
activateis a function. Call it when the experience should start. Convert then checks the location's conditions.options.locationIdis the location's ID.options.isActiveistruewhen the location is already active.
The script runs the callback again each time it runs, which includes every route change when SPA optimizations are on. This example starts the experience once a button appears:
if (options.isActive) return;
var tries = 0;
var timer = setInterval(function () {
tries++;
if (document.querySelector(".checkout-button")) {
clearInterval(timer);
activate();
} else if (tries > 50) {
clearInterval(timer);
}
}, 100);
For more callback examples, see Targeting Experiments with Dynamic Location Triggers (JS Callbacks).
Run One Experience by Hand
If your experience's location uses Upon Run or When a callback is called, your code can ask the script to check that one experience again and start it. Use this when you can't use a trigger. A common setup is a location with a JS Condition that becomes true when your app reaches the right moment. See Manually activate experiment and Targeting Experiments and Firing Goals Based on a JavaScript Condition.
window._conv_q = window._conv_q || [];
window._conv_q.push({
what: "executeExperience",
params: {
experienceId: "100123456", // replace with your experience's ID
triggerIntegrations: false // optional
}
});
Find the ID in Where Do I Find my Experiment and Variation IDs.
- The script checks the experience's locations and audiences again. If they don't match, nothing starts.
- It only reaches locations whose trigger is Upon Run or When a callback is called. For a Manually or DOM Element location, use
triggerLocationinstead. - URL conditions are compared with the URL the script read the last time it ran. With SPA optimizations on, that updates after the page changes. With them off, it can still be the URL from when the page loaded. For a location you start this way, use a JS Condition that reads the current page, such as
window.location.pathname === "/pricing". triggerIntegrations: falsekeeps Convert from sending the experience view to your analytics integrations each time you call it.
In older code you may see executeExperiment. It's the earlier name for the same call.
Run the Script Again
If Disable Single Page App Optimizations is checked under Configuration > More Settings, the script no longer runs again by itself when the URL changes. Clearing the checkbox is the simplest fix. See SPA Optimizations. If you need to keep it checked, call run from your app each time the route changes:
window._conv_q = window._conv_q || [];
window._conv_q.push({what: "run"});
A run reads the current URL, runs your Global Javascript, removes what variations added, and checks locations, audiences and goals again. If you call it several times within half a second, the script runs for the first call and once more for the last one.
Check Goals Again
The script checks your goals each time it runs, including after a route change. If a goal depends on something that changes inside the page without a route change, or if SPA optimizations are off, ask for a goals check:
window._conv_q = window._conv_q || [];
window._conv_q.push({what: "recheckGoals"});
This reads the current URL and checks your goals. It doesn't check your experiences again. If you see ["recheck_goals"] in older code, it's the earlier name for the same call.
Frequently Asked Questions (FAQ)
Does Convert automatically re-check locations and goals in SPAs?
Yes, when SPA optimizations are on, which is the default. Each time the page changes, the script compares the address with the one it last saw. If it's different, the script runs again and checks locations, audiences and goals for the new URL. It doesn't depend on history.pushState or history.replaceState, so it works however your router changes the URL.
What if the URL changes but the page content stays the same?
The script looks at the URL when something on the page changes. If the URL changes and nothing on the page does, the script doesn't notice until the next change.
Do I still need a JS Condition instead of a URL condition?
No. With SPA optimizations on, the script checks URL conditions again for each route. Use a JS Condition when the condition depends on the state of your app, not on the address.
Does a change after the # in the URL count?
Yes. The script compares the full address, including the query string and everything after the #. A Page URL condition compares only the part before the ? and the #. To target a route that lives after a #, such as /#/pricing, use a JS Condition like window.location.hash === "#/pricing".
What if a variation makes the page slow or breaks navigation links?
See Advanced SPA Configuration: Tracking Script Optimization Tips. To turn SPA optimizations off, see SPA Optimizations.
If you have more questions about running experiences on your app, contact our support team.