Running Experiments on Single Page Apps

Running Experiments on Single Page Apps (SPAs)

For running experiments on SPAs, you will need a developer to help you set up the experiments.

Experiments on SPAs usually need to be handled differently than other experiments.  Because the Convert script usually cannot read the URL that a website visitor is navigating, it cannot trigger experiments with standard methods. 

However, Convert provides other methods for triggering experiments within single-page frameworks:

1) Triggering Polling

Polling is the process by which the experience conditions are tested to determine if said experience should be triggered. This includes monitoring the visitor URL, audience conditions or JavaScript conditions to run the test. Polling is usually triggered by Convert when a new page is loaded. On SPAs, usually no new pages are loaded on the web app. In that case, you would need the following code to start the polling:

_conv_q = _conv_q || [];

You should determine what the best event on your SPA would be to trigger the above code. For example, on an Angular page you can add the above snippet inside your Angular controller, similar to the following:

$rootScope.$on("$locationChangeSuccess", function(event, next, current) {
         _conv_q = _conv_q || [];  

2) Use JavaScript Conditions in the Site Area

Because the Convert script cannot read URL changes in an SPA, you should use a JavaScript condition instead of a URL match condition to trigger an experiment. You can find a more thorough explanation on how to do this in the following article.

3) Manually Activate an Experiment

You can trigger experiments manually after you determine that a certain flow has happened. In this method, Site Area and Audience conditions will still be tested after triggering the polling with code. Please read the following article about how this can be done.

With the above 3 methods, you should be able to trigger experiments at the right moment in an Angular app.

** Be aware that changes triggered by the experiment will not be reset as they would be with a normal web page. For example, if you change a page element after a menu element has been clicked, clicking on the home page menu element will not reset that change. You will have to undo the element with code. You may want to do this by (for example) adding code to the Custom JavaScript area within the Visual Editor. 

4) Goals checked through polling

The majority of goals are checked by Convert throughout the polling process. This polling process occurs when a page is initially loaded and is done by the Convert script. However, with SPAs such as Angular, page loading does not occur when navigating through the application. For these types of scenarios, Convert has a specific function to call to activate the polling for monitoring goal conditions. The code for calling it is the following:

window._conv_q = window._conv_q || [];

Using dataLayer events to keep page state

We see many customers using dataLayer events to keep track of the website state. You can use these dataLayer events to trigger experiments with polling or any of the methods mentioned above. You can configure Google Tag Manager (GTM) to trigger the polling code or manual experiment activation code when an event is pushed onto the dataLayer. Please reference GTM documentation or contact Convert support if you need help.

Please don't hesitate to have a chat with us via any of the Support channels if you have any further questions about configuring these types of applications with Convert.

keyword: vue


Have more questions? Submit a request