Rich-text Reply

Creation of a real time Audience from page level JavaScript variables

ScottKing 07-07-16

Creation of a real time Audience from page level JavaScript variables

Hi guys, looking for some technical guidance - hopefully a simple one - how can I pass audience data from a page to Optimziely?

 

More specifically how can I pass a key/value pair from an on page JavaScript variable to Optimizely so that I can create an Audience / segment and return an on page experience utilizing said segment derived from the key/value pair.

 

Eg:

 

  1. Page begins to load

 

  1. JavaScript variable contains something like;

Var FaveFood(['favorite food', 'Hamburger buns'])

 

  1. Send key/value 'favorite food', 'Hamburger buns' to Optimizely

 

  1. Return an experience for Audiences with a 'favorite food' equal to 'Hamburger buns'

 

 

Step 3 is the one I’m trying to work how to do. I know how to do Step 4 via the Optimizely UI, no question there. I’m just trying to find the method to send the key/value pair to Optimziely.

 

Thanks guys!

 

 

robertchan 07-07-16
 

Re: Creation of a real time Audience from page level JavaScript variables

Have a look at this article: https://help.optimizely.com/Target_Your_Visitors/Custom_JavaScript_audience_targeting

 

According to Optimizely:

If you are using a specific variable defined on your page for Custom JavaScript Targeting, the variable MUST exist on the page ABOVE Optimizely. Otherwise, when Optimizely executes the condition will always evaluate to “false” since the variable or element does not yet exist.

 

Then in audience conditions, specify custom JavaScript by checking for the existence of your variable. For example, FaveFood.length.

Robert Chan

Experimentation Hero
ScottKing 07-08-16
 

Re: Creation of a real time Audience from page level JavaScript variables

Robert, thank you very much for your response.

I take it that the Optimizely script will read and access any JavaScript variables that are available at the window/document level (technically any JS variable as JS doesn't technically support variable scope like most languages). So long as the variable is declared / lives above the Optimizely embed code it can access the variable.

.. Makes sense. Thank you!