- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Audience Custom JS Issue

Audience Custom JS Issue
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
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!
Solved! Go to Solution.

Re: Audience Custom JS Issue
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
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.
Re: Audience Custom JS Issue
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
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!
Marc