Running Experiments on an AngularJS Page

Running Experiments on AngularJS

For running experiments on any version of AngularJS (herein referred to simply as "Angular"), you will need a developer to help you set up the experiments.

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

However, Convert provides other methods to trigger experiments within single-page frameworks such as Angular.

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. However, because Angular is a single-page framework, no new pages are loaded on an Angular app. You would need the following code to start the polling.

_conv_q = _conv_q || [];

You should determine what the best Angular event is to trigger the above code. For example, on your 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 Angular app, 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 could 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 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

A majority of goals are checked in Convert through the polling process. This polling process occurs when a page is initially loaded and is done by the Convert script. However, with single-page applications 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 only 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 contact us via the Support channels if you have any further questions about configuring these types of applications with Convert. We love to help.

Have more questions? Submit a request