- Help Center
- Developers
- Custom JavaScript
-
General
-
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
-
Shopify
-
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
- Inspectlet
- Crazy Egg
- LanderApp
- Unbounce
- Instapage
- Drupal
- PrestaShop
- Magento
- Roistat
- Piano Analytics
- Heap Analytics
- Kissmetrics
- Mouseflow
- Adobe Analytics
- Clicky
- Quantum Metric
- Google Campaign
- Checkout Champ
-
Account Management
-
Developers
-
What's New
-
Common Questions
JavaScript Conditions Use Cases
🚀 This Article Will Help You:
- Where Javascript can be implemented?
- Referral URL
- URL/Parameter/Query String :: Contains
- URL using Regex
- Cookies
- Variables
- Elements
- Screen Size
- Min/Max Screen Size
JavaScript is the most widely used scripting language on earth. Javascript can be used with Convert in a diversity of cases. This article is all about JS conditions examples that can be used targeting URLs, cookies, variables, and others. The codes are from real use case scenarios.
🛠️ Where Javascript can be implemented?
Basically everywhere, Goals, Audiences, Locations, Experience, Variation, and Project.
Comparison Operators
Case: x = 5 and y = 3
Operator | Description | Comparing | Returns |
---|---|---|---|
== | equal to | x == 8 | false |
x == 5 | true | ||
=== | equal value and equal type | x === "5" | false |
x === 5 | true | ||
!= | not equal | x != 8 | true |
!== | not equal value or not equal type | x !== "5" | true |
x !== 5 | false | ||
> | greater than | x > 8 | false |
< | less than | x < 8 | true |
>= | greater than or equal to | x >= 8 | false |
<= | less than or equal to | x <= 8 | true |
&& | and | (x < 10 && y > 1) | is true |
|| | or | (x === 5 || y === 5) | is false |
source: https://www.w3schools.com/
Examples:
🔗 Referral URL
// Equal
document.referrer == "YourURL.com"
// Not Equal
document.referrer != "YourURL.com"
// Contains
document.referrer.includes('somethingtocheck') == true/false
🌍 URL/Parameter/Query String :: Contains
window.location.href.includes('yourstring') == true
window.location.href.includes('\?yourstring') || window.location.href.includes('\&yourstring') == true
window.location.href.includes('yourstring') && window.location.href.includes('yourstring2') == true
📝 URL using Regex
//Starts with
window.location.href.match(/https?:\/\/(www.yourdomain.com\//).length > 0
//Using wildcards (same as contains)
window.location.href.match(/(.*)\/(child\/child1\/child2)(|\/)(.*)/).length > 0
🍪 Cookies
//Cookie is present
document.cookie.indexOf('cookieName') > -1
//Cookie is not present
document.cookie.indexOf('cookieValue') > -1 == false
// Cookie is present and contains
document.cookie.indexOf('cookieName') > -1 && document.cookie.indexOf('cookieValue') > -1
📊 Variables
//Variable is present
typeof yourvariable != 'undefined'
//Variable is not present
typeof yourvariable == 'undefined'
//Variable contains/not contains
yourvariable.includes('valuetocheck') == true/false
📌 Elements
//Element is present
document.getElementById("intro") != null
document.getElementsByClassName("intro") != null
convert.$('.selector').length != 0
//Element is not present
document.getElementById("intro") == null
document.getElementsByClassName("intro") == null
convert.$('.selector').length == 0
📏 Screen Size
screen.width == 1920
screen.height == 1080
📐 Min/Max Screen Size
window.matchMedia("(min-width: 768px)").matches
window.matchMedia("(max-width: 1024px)").matches