Rich-text Reply

Pushing Custom Tags: AJAX Loaded

markhayden 08-18-14

Pushing Custom Tags: AJAX Loaded

Is it possible to push customTags or re-initialize optimizely after an action has been taken? So say I want to add a customTag if/after a user has pressed a button or taken an action. As it stands I can't push customTags after the initial load. Any way to pull this off so I can change content / audiences on the fly based on interaction? Thanks in advance!

cubelodyte 08-19-14
 

Re: Pushing Custom Tags: AJAX Loaded

[ Edited ]

If I understand the question correctly, I believe it's a matter of setting your experiment to "Manual" activation mode and then forcing activation.

 

This takes 2 parts:

  1. First you have to provide the activate command immediately after the Optimizely snippet in your page code
  2. Then you have to kick fire the API activation again after your AJAX call. 

 

All of this can be part of your site code if you think you'll do it on a regular basis.

 

For our site, we have the API activation immediately after the code snippet than we add the manual AJAX activation in the Global.JS.

 

An example is to add a document ready function in the Global.js like this:

 

$( document ).ready(function() {
  window.$( document ).ajaxComplete(function(event,xhr,options) {
    var ajURL = options.url;
    var rxURL = new RegExp('^\/v\\d\/path1\/new'); //Dynamically generated content

    if (rxURL.test(ajURL)) {

    window.optimizely.push(["activate"]);
    window.optimizely.push(['trackEvent', 'lead_popup_any']);
  }
  else {
    rxURL = new RegExp('^\/v\\d\/service\/ajax\.js'); //AJAX Service Response
    if (rxURL.test(ajURL)) {
      window.optimizely.push(['trackEvent', 'kpi_submit']);
        if (xhr.status==200) {
          window.optimizely.push(['trackEvent', 'kpi_confirmed']);
        }
      else if (xhr.status==422) {
        window.optimizely.push(['trackEvent', 'kpi_failed']);
      }
    }
    else {
      rxURL = new RegExp('^/responsePage/'); //Thank You Response
      if (rxURL.test(ajURL)) {
        window.optimizely.push(['trackEvent', 'kpi_responsePage']);
      }
    }
  }
}

 

Scott Ehly
Manager of Site Optimization
sehly@rentpath.com

'The single biggest problem with communication is the illusion that it has taken place.' - George Bernard Shaw
markhayden 08-19-14
 

Re: Pushing Custom Tags: AJAX Loaded

Thanks for taking the time to respond but I don't think that is quite it. The challenge isn't the manual / automatic activation, its pushing customTags after the initial load. It appears that once Optimizely has been added to the DOM it no longer allows you to push in customTags. What we are looking to do is add those customTags after the initial load and then trigger one of the manually activated experiments as mentioned by CUBELODYTE. Thanks!
cubelodyte 08-19-14
 

Re: Pushing Custom Tags: AJAX Loaded

Mark, I had been interpreting "customTag" to mean custom event in Goal Tracking.

However, if you read past the initial "activate" code in my example, the rest is all about pushing custom tracking events. I would be surprised to find out that you couldn't set your customTag conditions using the ajaxComplete event and then call the "activate". Every "activate" call should re-interpret the audience conditions.
Scott Ehly
Manager of Site Optimization
sehly@rentpath.com

'The single biggest problem with communication is the illusion that it has taken place.' - George Bernard Shaw