Skip to content

Commit 891dd38

Browse files
committed
fix analytics custom trigger example
1 parent d953380 commit 891dd38

1 file changed

Lines changed: 24 additions & 74 deletions

File tree

docs/campaign-cart/analytics/examples/custom-analytics-triggers.md

Lines changed: 24 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ Here's the complete JavaScript code for the custom analytics triggers:
133133
* - dl_begin_checkout: Fires when carts.create fires (name/email input) OR when express checkout is clicked
134134
*
135135
* The events use the same format as SDK defaults:
136-
* - dl_ prefix (Campaign Cart SDK's standard naming convention)
136+
* - dl_ prefix for Elevar compatibility
137137
* - Same ecommerce data structure
138-
* - Pushed to both ElevarDataLayer (for Elevar compatibility) and dataLayer (standard GTM)
138+
* - Pushed to both ElevarDataLayer and dataLayer
139139
*
140140
* Usage: Include this script after the SDK loader, and block the default events
141141
* in your config.js: blockedEvents: ['dl_add_to_cart', 'dl_begin_checkout']
@@ -144,48 +144,6 @@ Here's the complete JavaScript code for the custom analytics triggers:
144144
(function() {
145145
'use strict';
146146

147-
// Flag to allow our script's InitiateCheckout events through
148-
let isFiringFromOurScript = false;
149-
150-
// Wrap fbq immediately to block SDK's InitiateCheckout on page load
151-
(function() {
152-
const originalFbq = window.fbq;
153-
154-
// Create wrapper function
155-
window.fbq = function() {
156-
const args = Array.from(arguments);
157-
const command = args[0];
158-
const eventName = args[1];
159-
160-
// Block InitiateCheckout unless it's from our script
161-
if (command === 'track' && eventName === 'InitiateCheckout' && !isFiringFromOurScript) {
162-
console.log('🚫 Blocked SDK InitiateCheckout event (backup blocker)');
163-
return;
164-
}
165-
166-
// Allow our script's events through
167-
if (command === 'track' && eventName === 'InitiateCheckout' && isFiringFromOurScript) {
168-
isFiringFromOurScript = false; // Reset flag after allowing
169-
}
170-
171-
// Call original fbq
172-
if (typeof originalFbq === 'function') {
173-
return originalFbq.apply(this, arguments);
174-
}
175-
176-
// If originalFbq wasn't available, queue to _fbq
177-
window._fbq = window._fbq || [];
178-
window._fbq.push(args);
179-
};
180-
181-
// Copy properties from original fbq if it exists
182-
if (originalFbq) {
183-
Object.keys(originalFbq).forEach(function(key) {
184-
window.fbq[key] = originalFbq[key];
185-
});
186-
}
187-
})();
188-
189147
// Track if events have been fired to prevent duplicates
190148
let hasFiredAddToCart = false;
191149
let hasFiredBeginCheckout = false;
@@ -328,12 +286,12 @@ Here's the complete JavaScript code for the custom analytics triggers:
328286
const calculatedValue = items.reduce((sum, item) => sum + (item.price * item.quantity), 0);
329287
const value = totalValue || calculatedValue;
330288

331-
// Fire to GTM (dataLayer) - using dl_ prefix (Campaign Cart SDK's standard naming convention)
289+
// Fire to GTM (dataLayer) - using dl_ prefix for Elevar compatibility
332290
if (typeof window.dataLayer !== 'undefined') {
333-
// Ensure ElevarDataLayer exists (for Elevar compatibility - optional if not using Elevar)
291+
// Ensure ElevarDataLayer exists
334292
window.ElevarDataLayer = window.ElevarDataLayer || [];
335293

336-
// Create event in SDK format (dl_ prefix is Campaign Cart SDK's standard)
294+
// Create event in SDK format (dl_ prefix for Elevar)
337295
const event = {
338296
event: 'dl_add_to_cart',
339297
ecommerce: {
@@ -344,17 +302,17 @@ Here's the complete JavaScript code for the custom analytics triggers:
344302
timestamp: new Date().toISOString()
345303
};
346304

347-
// Push to ElevarDataLayer first (for Elevar processing - optional if not using Elevar)
305+
// Push to ElevarDataLayer first (for Elevar processing)
348306
window.ElevarDataLayer.push(event);
349307

350308
// Clear previous ecommerce data and push to standard dataLayer
351309
window.dataLayer.push({ ecommerce: null });
352310
window.dataLayer.push(event);
353311

354-
console.log('Fired dl_add_to_cart to GTM');
312+
console.log('Fired dl_add_to_cart to GTM (Elevar format)');
355313
}
356314

357-
// Fire to Facebook Pixel - simple direct call
315+
// Fire to Facebook Pixel
358316
if (typeof window.fbq !== 'undefined') {
359317
const fbParams = {
360318
content_type: 'product',
@@ -371,7 +329,7 @@ Here's the complete JavaScript code for the custom analytics triggers:
371329
};
372330

373331
window.fbq('track', 'AddToCart', fbParams);
374-
console.log('Fired add_to_cart to Facebook Pixel');
332+
console.log('Fired add_to_cart to Facebook Pixel');
375333
}
376334

377335
hasFiredAddToCart = true;
@@ -404,12 +362,12 @@ Here's the complete JavaScript code for the custom analytics triggers:
404362
// Get coupon if available
405363
const coupon = cartData.appliedCoupons?.[0]?.code || (Array.isArray(cartData.appliedCoupons) && cartData.appliedCoupons.length > 0 ? cartData.appliedCoupons[0] : null) || null;
406364

407-
// Fire to GTM (dataLayer) - using dl_ prefix (Campaign Cart SDK's standard naming convention)
365+
// Fire to GTM (dataLayer) - using dl_ prefix for Elevar compatibility
408366
if (typeof window.dataLayer !== 'undefined') {
409-
// Ensure ElevarDataLayer exists (for Elevar compatibility - optional if not using Elevar)
367+
// Ensure ElevarDataLayer exists
410368
window.ElevarDataLayer = window.ElevarDataLayer || [];
411369

412-
// Create event in SDK format (dl_ prefix is Campaign Cart SDK's standard)
370+
// Create event in SDK format (dl_ prefix for Elevar)
413371
const event = {
414372
event: 'dl_begin_checkout',
415373
ecommerce: {
@@ -425,17 +383,17 @@ Here's the complete JavaScript code for the custom analytics triggers:
425383
event.ecommerce.coupon = coupon;
426384
}
427385

428-
// Push to ElevarDataLayer first (for Elevar processing - optional if not using Elevar)
386+
// Push to ElevarDataLayer first (for Elevar processing)
429387
window.ElevarDataLayer.push(event);
430388

431389
// Clear previous ecommerce data and push to standard dataLayer
432390
window.dataLayer.push({ ecommerce: null });
433391
window.dataLayer.push(event);
434392

435-
console.log('Fired dl_begin_checkout to GTM');
393+
console.log('Fired dl_begin_checkout to GTM (Elevar format)');
436394
}
437395

438-
// Fire to Facebook Pixel - set flag to allow through wrapper
396+
// Fire to Facebook Pixel
439397
if (typeof window.fbq !== 'undefined') {
440398
const fbParams = {
441399
content_type: 'product',
@@ -454,10 +412,8 @@ Here's the complete JavaScript code for the custom analytics triggers:
454412
fbParams.coupon = coupon;
455413
}
456414

457-
// Set flag to allow our event through the wrapper
458-
isFiringFromOurScript = true;
459415
window.fbq('track', 'InitiateCheckout', fbParams);
460-
console.log('Fired begin_checkout to Facebook Pixel');
416+
console.log('Fired begin_checkout to Facebook Pixel');
461417
}
462418

463419
hasFiredBeginCheckout = true;
@@ -512,7 +468,7 @@ Here's the complete JavaScript code for the custom analytics triggers:
512468
* Setup event listeners for triggers
513469
*/
514470
function setupTriggers() {
515-
console.log('🔧 Setting up custom analytics triggers');
471+
console.log('Setting up custom analytics triggers');
516472

517473
// 1. Fire add_to_cart when checkout page loads
518474
if (isCheckoutPage()) {
@@ -533,20 +489,14 @@ Here's the complete JavaScript code for the custom analytics triggers:
533489

534490
// 2. Fire begin_checkout when prospect cart is created (name/email input)
535491
document.addEventListener('next:prospect-cart-created', function(event) {
536-
// Check if form has user input (not page load)
537-
const emailField = document.querySelector('[data-next-checkout-field="email"], [os-checkout-field="email"], input[type="email"]');
538-
539-
if (emailField && emailField.value && emailField.value.trim().length > 0) {
540-
console.log('✅ Prospect cart created (user input detected), firing begin_checkout');
541-
setTimeout(function() {
542-
fireBeginCheckout();
543-
}, 100);
544-
} else {
545-
console.log('⚠️ Prospect cart created but no user input - ignoring (likely page load)');
546-
}
492+
console.log('Prospect cart created, firing begin_checkout');
493+
setTimeout(function() {
494+
fireBeginCheckout();
495+
}, 100);
547496
});
548497

549498
// 3. Fire begin_checkout when express checkout is clicked
499+
// Listen for express checkout button clicks
550500
document.addEventListener('click', function(event) {
551501
const target = event.target;
552502
if (!target) return;
@@ -555,7 +505,7 @@ Here's the complete JavaScript code for the custom analytics triggers:
555505
const button = target.closest('[data-next-express-checkout], [os-express-checkout], button[data-next-payment="paypal"], button[data-next-payment="apple_pay"], button[data-next-payment="google_pay"]');
556506

557507
if (button) {
558-
console.log('Express checkout clicked, firing begin_checkout');
508+
console.log('Express checkout clicked, firing begin_checkout');
559509
setTimeout(function() {
560510
fireBeginCheckout();
561511
}, 100);
@@ -565,7 +515,7 @@ Here's the complete JavaScript code for the custom analytics triggers:
565515
// Also listen for express checkout started event (if SDK emits it)
566516
if (typeof window.next !== 'undefined' && window.next.on) {
567517
window.next.on('express-checkout:started', function() {
568-
console.log('Express checkout started event, firing begin_checkout');
518+
console.log('Express checkout started event, firing begin_checkout');
569519
setTimeout(function() {
570520
fireBeginCheckout();
571521
}, 100);

0 commit comments

Comments
 (0)