Widget Custom JavaScript Code

Written By Aaron Fessler (Super Administrator)

Updated at October 23rd, 2025

Overview

Custom JavaScript code allows you to add tracking pixels, analytics events, or other custom behavior to your booking widgets beyond the standard integrations TripWorks provides. You write JavaScript code (not HTML) that executes at key points in the booking journey.

When to Use Custom Code vs. Integrations

For most tracking needs, use TripWorks' built-in integrations:

  • Google Analytics - Automatic event tracking
  • Google Ads - Conversion tracking
  • Facebook/Meta Pixel - Facebook advertising events
  • TikTok Pixel - TikTok advertising events

Custom JavaScript code is for advanced use cases not covered by standard integrations, such as:

  • Custom analytics platforms
  • Proprietary tracking systems
  • Specialized conversion tracking
  • Third-party marketing tools

Contact Your Account Manager

You cannot add custom JavaScript code yourself. Contact your TripWorks account manager to discuss your tracking needs and determine if custom code is necessary.

 

When Custom Code Runs

Custom JavaScript code only runs for live customers on your published booking widget. It does not run:

  • ❌ When logged-in team members use the widget
  • ❌ In preview mode
  • ✅ Only in production for actual customers

This ensures your analytics data remains clean and accurate.


Custom Code Events

You can add JavaScript code that executes at four key points in the booking journey:

1. On Widget Load

Executes when the booking widget first loads on the page.

Use cases: Page view tracking, initial session setup

2. On View Item

Executes when a customer views an experience detail page.

Use cases: Product view tracking, browse behavior analytics

3. On Add to Cart

Executes when a customer adds an experience to their cart.

Use cases: Add-to-cart events, funnel tracking

4. On Conversion

Executes when a booking is successfully completed.

Use cases: Purchase tracking, conversion pixels, revenue reporting


Available Variables

All custom code receives a data object with contextual information. Access variables using data.variableName.

Standard Variables (Available in All Events)

Variable Description Example Value
confirmationCode Trip confirmation code "ABC123"
total Total amount with fees (zero-decimal format*) 12500 (= $125.00)
subtotal Subtotal without fees (zero-decimal format*) 10000 (= $100.00)
currency ISO currency code "USD", "EUR", "GBP"
widgetId Widget UUID "a1b2c3d4-..."
widgetName Widget name "Main Booking Widget"
utm_source UTM source parameter from URL "google"
utm_medium UTM medium parameter from URL "cpc"
utm_campaign UTM campaign parameter from URL "summer-sale"
utm_term UTM term parameter from URL "kayak-tours"
utm_content UTM content parameter from URL "ad-variant-a"
landingUrl Current page URL "https://example.com/book"
referralUrl Referrer URL (where customer came from) "https://google.com"
eventType Event name "on_load", "on_conversion"
timestamp ISO 8601 timestamp "2025-10-23T14:30:00.000Z"

*Zero-Decimal Format: Monetary values are provided without decimal points to avoid floating-point precision issues. Multiply by 100 for cents/pence.

Examples:

  • $125.00 USD12500
  • €50.50 EUR5050
  • £199.99 GBP19999

Event-Specific Variables

On View Item & On Add to Cart

Variable Description Example Value
experience_id Numeric experience ID 12345
experience_name Experience name "Sunset Kayak Tour"
experience_slug URL-friendly experience identifier "sunset-kayak-tour"

On Add to Cart (Additional)

Variable Description Example Value
timeslot_id Numeric timeslot ID (if applicable) 67890

On Conversion (Additional)

Variable Description Example Value
transaction_id Same as confirmationCode "ABC123"
value Same as total (for convenience) 12500

Code Examples

Example 1: Log Data to Console (Debugging)

// On Conversion
console.log('Booking completed!', {
  confirmationCode: data.confirmationCode,
  total: data.total / 100, // Convert to dollars
  currency: data.currency
});

Example 2: Push Event to Google Tag Manager

// On Conversion
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'tripworks_custom_purchase',
  transactionId: data.confirmationCode,
  transactionTotal: (data.total / 100).toFixed(2),
  transactionCurrency: data.currency,
  utmSource: data.utm_source,
  utmMedium: data.utm_medium,
  utmCampaign: data.utm_campaign
});

Example 3: Track Experience View

// On View Item
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'view_item',
  ecommerce: {
    items: [{
      item_id: data.experience_id,
      item_name: data.experience_name,
      item_category: 'Experience'
    }]
  }
});

Example 4: Track Add to Cart

// On Add to Cart
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'add_to_cart',
  ecommerce: {
    items: [{
      item_id: data.experience_id,
      item_name: data.experience_name
    }]
  }
});

Example 5: Custom Conversion Pixel

// On Conversion
var img = document.createElement('img');
img.src = 'https://tracking.example.com/pixel?transaction=' +
  encodeURIComponent(data.confirmationCode) +
  '&value=' + (data.total / 100) +
  '&currency=' + data.currency;
img.style.display = 'none';
document.body.appendChild(img);

⚠️ Avoid Duplicate Tracking

If you have TripWorks integrations enabled (Google Analytics, Facebook Pixel, etc.) and you add custom code for the same platform, you may receive duplicate events.

Example: If you have the Google Analytics integration turned on in TripWorks and you add Google Analytics code to your custom JavaScript, every conversion will be tracked twice.

Best Practice:

  1. Use TripWorks' built-in integrations whenever possible
  2. Only add custom code for platforms not supported by TripWorks integrations
  3. Disable TripWorks integrations if you prefer to manage all tracking via custom code
 

Getting Help

To enable custom JavaScript code for your widgets:

  • Contact your TripWorks account manager
  • Describe your tracking requirements
  • Your account manager will configure the custom code for you

For technical support:

  • Email: support@tripworks.com
  • Include your widget name and tracking requirements