Rich-text Reply

Re-running javascript in a Single Page Application

marksteyn 10-12-16

Re-running javascript in a Single Page Application

I'm trying to integrate Optimizely into a SPA based on Backbone. I'm manually activating pages based on events from the Backbone router, and I've created a test experiment which simply changes the colour of an element on the page. When I navigate to the page of the experiment for the first time, I can see that the experiment runs. When I navigate away to a different section of the site, and then back to the page of the experiment, it doesn't run a second time, and in the console I see this message in the console: "Not activating Page, already active..." Because pages in the app are recreated when they are navigated to, modifications made by the javascript in the experiment are not applied when navigating to the page any time after the first. Is there a way to ensure that the javascript of the experiment runs each time the corresponding page is activated, rather than just the first?
robertchan 10-12-16
 

Re: Re-running javascript in a Single Page Application

Yes, you need to attach an event that fires on each page to let Optimizely know that a page has changed. To point you in the right direction, you can visit Settings > JavaScript > Project JavaScript within your project. Here, you can create some sort of function that polls for changes on every page.
Robert Chan

marksteyn 10-12-16
 

Re: Re-running javascript in a Single Page Application

That's pretty much my understanding too. I'm just not sure what actually needs to be run to inform Optimizely. I thought it was just a matter of calling the page activation, but it seems that's not the case.