- Help Center
- Target Visitors
- JavaScript Conditions
-
Getting Started
-
Configuration
- Targeting
- Split URL
- Product Testing
- Full Stack
- Experiment Management
- CSP Configuration
- Experiment Execution
- Reports
- Exit Popups
- GTM Integration
- Troubleshooting
- Performance Optimization
- Event-Triggered Changes
- Holdout Groups
- Split URL Pages
- URL Parameters
- DataLayer
- Menu Configurations
- Traffic Exclusion
- Experiment Scheduling
- Dynamic Element Changes
- Price Targeting
- Experience Scheduling
- Privacy
- Hash Changes
- Async Tracking
- Selective Installation
- CSS Selectors
- Vue.js Integration
- Page Content
- Multipage Split URL
- Organic Traffic
- Visual Editor
- Server-Side Testing
- Traffic Bucketing
- GDPR Warnings
- Statistical Confidence
- Browser Privacy
- Query Parameters
- Embedded Videos
- Tracking Code Execution
- Simultaneous Experiments
- Tags
- Deployments
- Disable Testing
- Locations
- Programmatic Bucketting
- Query Parameter Handling
- Convert Library
- Variation Previews
- Experiment Editing
- Opt-Out Script
- Data Reset
- Body Hiding
- Visit-Specific Variations
- Variation Styling
- Preview Issues
- Variation Editing
- Full-Site Testing
- Blinking Variations
- Cross-Domain Cookies
- Regex Support
- Conversion Tracking
- SPA Testing
- Project Setup
- Cross-Domain Tracking
- Geo-Targeting
- Analytics Tools
- Campaign Tags
- Previewing
- IDs
- Query String Targeting
- Bounce Rate Goals
- Bot Filtering
- Query String Variables
- Custom Audiences
- Redirects
- Baseline
- Tracking Code Location
- Secure Cookies
- AngularJS
- Cloudflare
- Code Installation
-
Track Goals
- Form Tracking
- Cookie Management
- iFrame Click Tracking
- Performance Optimization
- Revenue Tracking
- Interaction Goals
- Form Submissions
- Advanced Goals
- Lazy Loading
- Multi-Conversions
- URL Parameters
- Bounce Rate Goals
- DataLayer Integration
- Scroll Depth
- Social Interactions
- Page Views
- Marketo Forms
- Feature Analysis
- AJAX Forms
- Revenue Tracking via GTM
- Order Outliers
- Cumulative Revenue
- Goal Templates
- Adding Revenue Goals
- JS-Based Goals
- Goal Basics
- Google Analytics Goals
- Social Sharing
- Dynamic Goals
- Typeform Integration
-
Target Visitors
- Geolocation
- Interaction Goals
- Goal-Based Targeting
- Weather Targeting
- Cookie-Based Targeting
- Page Visits
- Audience Management
- Audience Segmentation
- Experiment Targeting
- Advanced Audience Creation
- Audience Templates
- Audience Creation
- Data Layer Integration
- Manual Activation
- JavaScript Conditions
- Device Targeting
- Language Targeting
- IP-Based Exclusion
- Visitor Management
- Page Tagging
- Cookies
-
Troubleshooting
- Google Warnings
- Visual Editor
- HTTPS Content
- Logs
- Support Options
- Bootstrap
- Cookie Blocking
- Change History
- Mobile Debugging
- AdWords
- Bot Exclusion
- Domain Issues
- Cloudflare Issues
- Monitoring
- Cloaking Penalties
- Goal Editor Issues
- Variations
- Snippet Performance
- Changes Not Saved
- Blocked Visual Editor
- Goal Testing
- Visual Editor Browsing
- Experiment Issues
- Installation Verification
- Data Leak Prevention
- Usage Limits
- Experiment Previews
- GA4 Revenue
- Chrome Debugger Logs
- SPA Errors
- Checkout JSON Error
-
Analyze Results
-
Integrations
- Google Analytics
- Cookie Consent Platforms
- Microsoft Clarity
- Plausible
- Marketo
- HubSpot
- Tealium
- Smartlook
- Klaviyo
- Salesforce CRM
- FullStory
- Snowplow Analytics
- Webflow
- GA4 Roles
- Amplitude
- Segment
- React
- BigCommerce
- WooCommerce
- Active Campaign
- Google Tag Manager
- Mixpanel
- Zapier
- Inspectlet
- Crazy Egg
- LanderApp
- Unbounce
- Instapage
- Drupal
- PrestaShop
- Magento
- Roistat
- Piano Analytics
- Heap Analytics
- Kissmetrics
- Mouseflow
- Adobe Analytics
- Clicky
-
Account Management
-
Developers
-
What's New
-
Common Questions
-
Shopify
Target experiment based on a custom Javascript condition that evaluates true at a later stage
Experiments are evaluated as soon as the Convert Experiences tracking script loads. Sometimes it's needed to fire an experiment based on values of variables that are defined later on the page (below the main tracking code inclusion point). This DMP (Data Management Platform) feature allows it to target, for example, lead score/stage from your Marketing Automation tool (like Marketo, Pardot, Elequou, Hubspot, or Salesforce) without much effort.
In that case, we can use a custom javascript condition targeting rule and an API call to re-check the experiment later.
Inside the JS condition, the following JS API function can be used:
convert_recheck_experiment()
When that code executes, a re-check of the experiment's conditions is scheduled for 50 ms later, for approximately the next two minutes, or until the experiment JS condition check is final (whichever comes first). Here is an example:
(function() {
if(typeof(window.my_variable)=="undefined") {
convert_recheck_experiment(); return false;}
else return (window.my_variable == "test_value");
})()
In the above example, we check if variable window.my_variable is defined at the run time; if not we call the API function to recheck in 50ms; if it's defined, we check its value against test_value and return true or false, depending on whether they match or not. The window.my_variable can be defined later on the page, after the main Convert tracking script, and the experiment will be checked and fired when that variable is defined.
Important
It is important to make sure that you take into account the possibility that a variable may not be defined at the time the code is first run; if that happens and an error is thrown, the rest of the code will not run.
An example is shown in the code above where we check to see if the variable is undefined, so that an error will not be returned and convert_recheck_experiment() will run.
The above type of targeting can be combined with the URL targeting type. If we wanted to fire the experiment like above but only for a page that has path /test_page.html:
- we would set the above condition into the Locations JS condition
- and we would add a URL targeting rule like below: URL does not contain /test_page.html. In doing so, we'd include the experiment pages where the JS condition is true but exclude all the pages that do not contain /test_page.html into the URL (therefore just the ones that contain /test_page.html will be included)
There are multiple use cases of this functionality limited only by the imagination of whoever uses it. It's worth mentioning one other common use case: fire the experiment when an element was added to the page (maybe via Ajax); the JS condition would look something like the below:
(function() {
if (document.querySelectorAll("element_selector").length === 0) {
convert_recheck_experiment();
return false;
} else {
return true;
}
})();
keywords: "DMP", "Data Management Platform"