Rich-text Reply

Conditional Activation - Performant Polling Functions

tomfuertes 05-01-15
Accepted Solution

Conditional Activation - Performant Polling Functions

Hey All! Just cross posting something from our weekly engineering blog here that we've seen a couple clients struggle with. We came up with a pattern that solved one of our main conditional activation pain points which was that the 50ms polling using the Polling Type activation mode didn't and/or couldn't be stopped after domReady (which was the majority of our use cases to date). Hope this helps someone! Short / sweet for devs - read our blog for a more in depth explanation if you have questions:

 

function(activate, options) {
  (function poll() { // named iife
    if ( $('#logged-in').length ) { // check for element
      activate(); // activate if exist
    } else if ( ! $.isReady ) { // else if not dom ready
      setTimeout(poll, 25); // recurse
    }
  })();
}

Original Post: http://clearhead.me/post/117861083520/ef-performant-conditional-activation

 

-Tom | Clearhead.me

____
- Tom Fuertes | CTO @ CROmetrics / LinkedIn
"Most Impactful Use of Personalization" and "Experience of the Year" Optie award winner.


Re: Conditional Activation - Performant Polling Functions

Thanks @tomfuertes - this is an approach we've just begun to surface with some of our clients as they also want to control how Conditional Activation's polling works.

 

A very interesting use case we just encountered was the reverse of this - do not begin polling UNTIL DOM ready.  The code looks something like this (essentially just reverse the if and else clauses from your example.):

 

function(activate, options) {
  var POLLING_INTERVAL = 50;
  var pollForReady = function() {
    if (document.readyState == 'complete' && /* CONDITION */) {
      activate();
    }
    else {
      // wait until both conditions are met
      setTimeout(pollForReady, POLLING_INTERVAL);
    }
  };
  pollForReady();
}

 We'd love to hear more from the Community on this topic - what are some ways you're pushing the boundaries of Conditional Activation?  Negative checks perhaps?

Harrison Krat
Solutions Architect | Optimizely, Inc.
harrison@optimizely.com
 

Re: Conditional Activation - Performant Polling Functions

[ Edited ]

Conditional Activation has been extremely valuable for us. Our sites are editorially driven and there are many cases where we want to target tests based on a certain editorial topic which usually isn't visible in the URL but is visible in the page's source code.

 

@Harrison_Krat- I believe the "negative check" solution you mention here is the one you came up with for us. In our case we had a test up that was targeted to every piece of editorial content on a site. But there were certain content types where we'd have more success with promos that were targeted based on that content's topic, rather than using the "sitewide" promo. We weren't able to target these topics from the URL so we used conditional activation to target separate promos to the specific topic we wanted and then inserted the "negative check" into the sitewide test so we wouldn't have overlapping promos running on the same page. It's worked out very well for us.

Aicke 05-29-15
 

Re: Conditional Activation - Performant Polling Functions

[ Edited ]

@Harrison_KratI'm curious why you are polling until domready and do not use $(document).ready() ?

 

function(activate, options) {
  $(document).ready(function() {
    var POLLING_INTERVAL = 50;
    var pollForReady = function() {
      if (/* CONDITION */) {
        activate();
      } else {
        setTimeout(pollForReady, POLLING_INTERVAL);
      }
    };
    pollForReady();
  });
}

 

Everything is hard, before it is easy.
Level 2

Re: Conditional Activation - Performant Polling Functions

Hi @Aicke,

 

Your approach is absolutely valid for the use case - thanks for demonstrating it!

 

In the particular conversation I had, we were looking for as much flexibility as possible to add or remove conditional checks.  Syntactically, it may be slightly easier to tease the DOM ready aspect out of the function if that check is not needed for a future use case.

Harrison Krat
Solutions Architect | Optimizely, Inc.
harrison@optimizely.com
 
Aicke 07-01-15
 

Re: Conditional Activation - Performant Polling Functions

@Harrison_Krat You've asked for examples of "pushing the boundaries of Conditional Activation".

 

I've written a solution I made for a task I had to solve, maybe that fits into your request:

Activating an experiment, if the user searches for specific words

Everything is hard, before it is easy.
Level 2

Re: Conditional Activation - Performant Polling Functions

HI @Aicke,

 

Your post certainly does fit into what we were looking for - thank you so much!  I replied to that post as well!

Harrison Krat
Solutions Architect | Optimizely, Inc.
harrison@optimizely.com