Rich-text Reply

Audience Custom JS Issue

mrcthms 08-06-15
Accepted Solution

Audience Custom JS Issue

So I have something really, relly puzzling going on with my Custom JS within an Audience.


The Custom JS seems to return false, or null, or nothing even though I know for a fact (by entering it in the console in the browser) that it does return true on the page I'm testing.


This is the code that's in the Custom JS:


parseInt(document.getElementById('header-cart-link').getAttribute('data-cart-total'), 10) !== 0;

The element in question definitely exists, the data-cart-total attribute definitely exists, and the total is definitely greater than 0.


Now, I thought that maybe the JS just wasn't running at all, but if I put something generic, like the following, the experiment runs:


[1, 2, 3].length === 3

So I now have literally no idea why this isn't running. Any help would be appreciated on this!

Level 2

JMarchant 08-06-15

Re: Audience Custom JS Issue

Hi mrcthms,


I believe the problem you're having is that Optimizely is evaluating the Audience before the element exists on the page. Audiences are only evaluated once, and if your test has immediate activation applied to it then this will be at the beginning of page load.


I would suggest using conditional activation instead. This will poll the page every 50ms to check if the condition evaluates to true.


I hope that helps.

James Marchant
Head of Development at

mrcthms 08-06-15

Re: Audience Custom JS Issue

Hey JMarchant,


That fixed the problem, thanks very much!


Would be handy to have that pointed out somewhere on the Custom JS Optiverse pages somewhere, I may email support with that suggestion.


Thanks again for your help!



Level 2