When to Use Script Rule
Script Rules are ideal when you need:- Complex conditional logic - Multiple conditions combined with AND/OR operators
- Geolocation-based routing - Direct visitors based on country, region, ISP, or connection type
- Behavior-based decisions - Route based on visit frequency, time between visits, or browsing patterns
- Customer segmentation - Different flows for existing customers vs new visitors
- Anti-fraud protection - Detect suspicious patterns like rapid page visits or VPN usage
- Device/Platform targeting - Advanced device detection beyond simple mobile/tablet/desktop
- Tag-based routing - Check for multiple tags with complex logic
- Click attribution - Route based on traffic source, click age, or IP matching
Script Rules execute on the server before the page loads, making them perfect for security-sensitive decisions that shouldn’t be exposed to the browser.
How Script Rules Work
- Server-Side Execution: Script Rules run on the server before the page loads
- Return Boolean: Your script must return
trueorfalse - Flow Control:
true→ Takes the Yes (left/top) pathfalse→ Takes the No (right/bottom) path
- Available Data: You have access to pre-loaded visitor data (see below)
Available Data Variables
Core Variables
is_whitelisted
Type: BooleanDescription: Whether the visitor is currently whitelisted
whitelist_source
Type: String | NullValues:
'cookie', 'cookie_blocked', 'expired_cookie', 'domain_all_whitelisted', nullDescription: How the whitelist status was determined
query
Type: ObjectDescription: All URL query parameters as key-value pairs
ip_address
Type: StringDescription: Visitor’s IPv4 address
ip_address6
Type: StringDescription: Visitor’s IPv6 address (if available)
referer / referrer
Type: StringDescription: HTTP referrer URL (both spellings available)
merchant_code
Type: StringDescription: Active payment processor code (e.g., ‘buygoods’, ‘clickbank’, ‘digistore24’)
Device Information
is_mobile
Type: BooleanDescription: Whether visitor is on a mobile device
is_tablet
Type: BooleanDescription: Whether visitor is on a tablet device
device
Type: ObjectDescription: Detailed device information Available Properties:
device.type- Device type (e.g., ‘smartphone’, ‘tablet’, ‘desktop’)device.brand- Device brand (e.g., ‘Apple’, ‘Samsung’, ‘Google’)device.model- Device modeldevice.os- Operating systemdevice.os_version- OS versiondevice.browser- Browser namedevice.browser_version- Browser version
Customer Data
is_customer
Type: BooleanDescription: Whether visitor has made a purchase
is_test_customer
Type: BooleanDescription: Whether customer purchase was marked as test
customer
Type: ObjectDescription: Customer information from last purchase Available Properties:
customer.name- Customer namecustomer.email- Customer emailcustomer.phone- Customer phonecustomer.country- Customer countrycustomer.zip- Postal codecustomer.city- Citycustomer.address- Street addresscustomer.order_id- Last order IDcustomer.order_item- Last purchased product codecustomer.order_item_name- Last purchased product namecustomer.order_ids- Array of all order IDscustomer.is_test- Whether it’s a test order
Session & Tracking
session.aff_id
Type: StringDescription: Affiliate ID from session
time / utc_timestamp
Type: Number (Unix timestamp in milliseconds)Description: Current server time
Tags
tags
Type: Array of StringsDescription: All tags assigned to the visitor (from
tag_ cookies)
IP Geolocation Data (Advanced)
To use IP geolocation data, your script must referenceip_whois - the system automatically loads this data only when needed for performance.
ip_whois
Type: ObjectDescription: Comprehensive IP geolocation and ISP information Available Properties:
ip_whois.country- Country code (e.g., ‘US’, ‘GB’, ‘DE’)ip_whois.country_name- Full country nameip_whois.region- Region/state codeip_whois.region_name- Full region nameip_whois.city- City nameip_whois.zip- Postal codeip_whois.lat- Latitudeip_whois.lon- Longitudeip_whois.timezone- Timezone (e.g., ‘America/New_York’)ip_whois.isp- Internet Service Providerip_whois.org- Organizationip_whois.as- Autonomous System numberip_whois.asname- AS nameip_whois.mobile- Is mobile carrierip_whois.proxy- Is proxy detectedip_whois.hosting- Is hosting providerip_whois.is_government- Is government IPip_whois.is_bot- Is known bot
Click Data (Advanced)
Click data is available when there’s a click ID present. This is commonly used on upsells but can be available on any page with click tracking.
click_data
Type: ObjectDescription: Information about the original ad click Available Properties:
click_data.time- Click timestamp (Unix seconds)click_data.seconds_ago- Seconds since clickclick_data.ip_address- Click IP address (IPv4)click_data.ip_address6- Click IP address (IPv6)click_data.ip_address_matches- Does current IP match click IP
Visit Data (Advanced)
To use visit data, your script must referencevisit_data - the system automatically loads visitor behavior metrics when referenced.
visit_data
Type: ObjectDescription: Comprehensive visitor behavior and history Available Properties:
visit_data.first_visit- First visit timestampvisit_data.time_since_first_visit- Milliseconds since first visitvisit_data.visit_count- Total number of visitsvisit_data.last_visit- Last visit timestampvisit_data.time_since_last_visit- Milliseconds since last visitvisit_data.ip_address- First visit IP (IPv4)visit_data.ip_address6- First visit IP (IPv6)visit_data.session_id- First session IDvisit_data.has_multiple_user_agents- Used different browsers/devicesvisit_data.unique_user_agent_count- Number of different user agentsvisit_data.page_visit_count_30min- Page visits in last 30 minutesvisit_data.page_visit_count_all_time- Total page visits evervisit_data.unique_pages_visited- Number of different pages visitedvisit_data.average_time_between_visits- Average time between visits (ms)visit_data.direct_visits_count- Visits without referrervisit_data.referrer_visits_count- Visits with referrervisit_data.rapid_visits_detected- Suspicious rapid visiting pattern
Debugging Script Rules
Using the Debug Window
The best way to debug Script Rules is with the Debug Window - a powerful tool that shows you exactly what’s happening when your script executes. How to Debug:- Open the Debugger - Click the “Debugger” button in the Page Builder or Page Events interface
- This will open your page with the Debug Window automatically activated
- Navigate to “Page Events” tab in the Debug Window to see Script Rule execution details
- Reload the page if needed to trigger the Script Rule
- Result:
TRUEorFALSE- which path was taken - Execution Time: How long the script took to run (in milliseconds)
- Next Path: Which branch the flow followed
- Console Messages: All your
console.log(),console.warn(), andconsole.error()output - Errors: Full error messages if the script failed
- Used IP Whois: Whether geolocation data was loaded
- Used Click Data: Whether click data was accessed
- Has Customer Data: Whether customer information was available
- Script Preview: First few lines of your script code
- Full Context: Complete evaluation data with all variable values
The Debug Window is the most efficient way to debug Script Rules. It shows you everything that happened during execution, including all console output and the exact data that was available.
Console Logging
You can useconsole.log() to debug your Script Rules:
Example Use Cases
Example 1: Geographic Targeting
Example 2: Customer Segmentation
Example 3: Anti-Fraud Protection
Example 4: Device & Platform Targeting
Example 5: Behavior-Based Routing
Example 6: Multi-Condition Affiliate Routing
Example 7: Time-Based Offers
Example 8: Tag-Based Progressive Disclosure
Best Practices
Performance Optimization
-
Avoid Heavy Operations When Possible
- Only reference
ip_whoisif you need geolocation - Only reference
click_dataif you need click attribution (requires click ID) - Only reference
visit_dataif you need behavior metrics
- Only reference
-
Simple Checks First
-
Use Tags for Behavior Tracking
Logic Clarity
-
Comment Your Code
-
Break Complex Logic into Steps
-
Use Meaningful Variable Names
Error Handling
-
Check for Null/Undefined
-
Provide Fallback Values
Testing
-
Test Both Paths
- Verify the
truepath works as expected - Verify the
falsepath works as expected - Test edge cases (missing data, null values, etc.)
- Verify the
-
Use Debug Window
- Click “Debugger” in Page Builder or Page Events to open Debug Window
- View console logs and script execution details in the “Page Events” tab
-
Test with Different IPs/Devices
- Use VPN to test different countries
- Use browser dev tools to simulate mobile devices
- Test with different referrers and query parameters
Troubleshooting
Script Returns Unexpected Results
Problem: Script always takes same path regardless of data Solutions:- Use the Debug Window - Click “Debugger” in the Page Builder or Page Events to see the actual result and all data
- Check your return statement - must return boolean
- Use
console.log()to debug variable values - Verify data is available (not null/undefined)
- Check for typos in property names
- The actual
Result(TRUE or FALSE) - All your console logs
- What data was actually available
- The exact path taken (
Next Path)
Script Execution Errors
Problem: Script fails and takesfalse path
Solutions:
- Check Debug Window - Click “Debugger” in the Page Builder to see the exact error message
- Check for syntax errors in your code
- Look at the error details in Debug Window → Page Events → script_rule event
- Ensure you’re not using unsupported JavaScript features
- Verify all referenced variables exist
- Click “Debugger” in the Page Builder or Page Events interface
- The Debug Window will open automatically
- Go to Page Events tab
- Find the
script_ruleevent with red/error status - Look at the Error field for the exact error message
- Check Full Context to see what data was available
Slow Performance
Problem: Page loads slowly when script rule executes Solutions:- Remove
ip_whoisreference if not needed - Remove
click_datareference if not needed (requires click ID to be present) - Remove
visit_datareference if not needed - Simplify logic to avoid heavy operations
Data Not Available
Problem:ip_whois, click_data, or visit_data is empty/null
Solutions:
- ip_whois: Make sure you reference it in your script (triggers lookup)
- click_data: Only available when there’s a click ID present
- visit_data: Data may not be available for first-time, brand-new visitors
JavaScript Limitations
Script Rules have some JavaScript limitations due to server-side execution:Supported Features
- Basic JavaScript syntax (ES5 + some ES6)
- Variables (
let,const,var) - Functions and arrow functions
- Arrays and objects
- Loops (
for,while,forEach) - Conditionals (
if,switch,ternary) - Comparison and logical operators
Math,Date,JSONobjectsStringandArraymethods (most common ones)console.log()for debugging
NOT Supported
- Async/await operations
- Promises
- Fetch API or HTTP requests
- Browser APIs (window, document, localStorage, etc.)
- ES6+ features like
flatMap,Object.fromEntries - Regular expressions with some advanced features
- Modules and imports
Polyfills Included
The following are polyfilled automatically:Array.prototype.includes()String.prototype.includes()
Related Documentation
- Debug Window - Learn how to debug Script Rules with the Debug Window
- Page Events - Overview of the Page Events system
- Query Parameter Conditions - Simpler alternative for URL parameter checks
- Content Visibility Control - Using whitelisting with containers
- Dynamic Content - Personalizing content based on conditions
- Exit Popups - Behavior-based exit intent detection
Script Rules are one of the most powerful features in Elastic Funnels. Start with simple logic and gradually add complexity as you become comfortable with the available data and patterns.