Created
April 29, 2024 21:01
-
-
Save antmbraun/2ff4094f45a7aff9f5619492102ac2da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file | |
* Fires Optimizely custom events when certain conditions are met. | |
* Knows which events to fire based on values in drupalSettings.optimizely_custom_events array which is populated in mb.module. | |
* | |
*/ | |
(function () { | |
Drupal.behaviors.optimizely_custom_events = { | |
attach: function () { | |
function optimizelyPush(event_name) { | |
window['optimizely'] = window['optimizely'] || []; | |
// Push the event to Optimizely. | |
try { | |
window['optimizely'].push({ | |
type: "event", | |
eventName: event_name, | |
}); | |
} | |
catch (err) { | |
if (typeof newrelic != "undefined") { | |
newrelic.noticeError(err); | |
} else { | |
console.log('newrelic not found'); | |
} | |
} | |
} | |
/* Marketo Form IDs */ | |
/* The listed form IDs should match those listed in tealiumSubmit.js */ | |
// Sales lead forms | |
// These records are sent to Salesforce via Marketo | |
const multistepFormIds = [ | |
1560, // FO - Get Started Multi-Step Demo | |
]; | |
const salesInquiryFormIds = [ | |
2535, // FO - ClassPass Guarantee | |
1068, // FO - Professional Services | |
1311, // FO - Sales Inquiry | |
1318, // FO - Sales Inquiry French | |
1319, // FO - Sales Inquiry German | |
1317, // FO - Sales Inquiry Spanish | |
]; | |
const salesReferralFormIds = [ | |
2473, // FO - Business Partner Self Serve (AKA Partner Referral Program) | |
1508, // FO - CAP Referral | |
]; | |
// Events that depend on Marketo forms being in the DOM. | |
// We use the Marketo API so that we can fire the Optimizely custom event when the form SUCCESSFULLY submits, not just when the submit button is clicked. | |
MktoForms2.whenReady(function (form) { | |
// Form submission events. | |
form.onSuccess(function () { | |
const formId = form.getId(); | |
let isSalesForm = false; | |
let optimizelyEvents = []; // Array of Optimizely event names to push. | |
// Add a generic event for ALL forms (even non-sales forms like the education subscribe form). | |
optimizelyEvents.push('form_conversion'); | |
// Multistep forms. | |
if (multistepFormIds.includes(formId)) { | |
isSalesForm = true; | |
optimizelyEvents.push('sales_form_conversion_multistep'); | |
} | |
// Sales Inquiry forms. | |
if (salesInquiryFormIds.includes(formId)) { | |
isSalesForm = true; | |
optimizelyEvents.push('sales_form_conversion_sales_inquiry'); | |
// The specific Optimizely event names depends on the type of page the user is on on. This is determined in mb.module, which populates the drupalSettings.optimizely_custom_events array. | |
if (typeof drupalSettings !== 'undefined' && typeof drupalSettings.optimizely_custom_events !== 'undefined') { | |
let customEvents = drupalSettings.optimizely_custom_events; | |
// Pricing page | |
if (customEvents.includes("sales_form_conversion_pricing")) { | |
optimizelyEvents.push('sales_form_conversion_pricing'); | |
} | |
// Subvertical pages | |
if (customEvents.includes("sales_form_conversion_subvertical")) { | |
optimizelyEvents.push('sales_form_conversion_subvertical'); | |
} | |
// Vertical pages | |
if (customEvents.includes("sales_form_conversion_vertical")) { | |
optimizelyEvents.push('sales_form_conversion_vertical'); | |
} | |
// Business Need pages | |
if (customEvents.includes("sales_form_conversion_business_need")) { | |
optimizelyEvents.push('sales_form_conversion_business_need'); | |
} | |
// Home page | |
if (customEvents.includes("sales_form_conversion_homepage")) { | |
optimizelyEvents.push('sales_form_conversion_homepage'); | |
} | |
} | |
} | |
// Referral forms. | |
if (salesReferralFormIds.includes(formId)) { | |
isSalesForm = true; | |
optimizelyEvents.push('sales_form_conversion_referral'); | |
} | |
// Generic event for all sales forms. | |
if (isSalesForm) { | |
optimizelyEvents.push('sales_form_conversion'); | |
} | |
// Submit events to Optimizely. | |
optimizelyEvents.forEach(function (eventName) { | |
optimizelyPush(eventName); | |
}); | |
}); | |
// Detect if user engages with the form. | |
let addFormEngagementEvents = function (formId, eventName) { | |
let form = document.querySelector('.marketo-form-' + formId); | |
if (form) { | |
form.addEventListener('focus', function (event) { | |
let formElement = form.closest('form'); | |
// Check if the event target is one of the form fields | |
if (!formElement.classList.contains('form-engagement') && event.target.matches('input, select, textarea')) { | |
optimizelyPush(eventName); | |
// Add class to form wrapper that we can check as a way prevent firing the event again. | |
form.closest('form').classList.add('form-engagement'); | |
} | |
}, true); // Using true for useCapture to handle the event in the capturing phase. | |
} | |
}; | |
// Sales form engagement on any page. | |
[...salesInquiryFormIds, ...multistepFormIds, ...referralFormIds].forEach(function (formId) { | |
addFormEngagementEvents(formId, 'sales_form_engagement'); | |
}); | |
// Sales form engagement on homepage. | |
if (typeof drupalSettings !== 'undefined' && typeof drupalSettings.optimizely_custom_events !== 'undefined') { | |
let customEvents = drupalSettings.optimizely_custom_events; | |
if (customEvents.includes("sales_form_engagement_homepage")) { | |
// 1311 is the form ID for the homepage sales form. | |
addFormEngagementEvents('1311', 'sales_form_engagement_homepage'); | |
} | |
} | |
}); | |
// CTA clicks - Pricing (all languages) | |
const starterCTAs = document.querySelectorAll("#page-plans-and-pricing .pricingPage__starterCta"); | |
if (starterCTAs) { | |
starterCTAs.forEach(function (cta) { | |
cta.addEventListener("click", function () { | |
optimizelyPush("click_pricing_starter_cta"); | |
}); | |
}); | |
} | |
const accelerateCTAs = document.querySelectorAll("#page-plans-and-pricing .pricingPage__accelerateCta"); | |
if (accelerateCTAs) { | |
accelerateCTAs.forEach(function (cta) { | |
cta.addEventListener("click", function () { | |
optimizelyPush("click_pricing_accelerate_cta"); | |
}); | |
}); | |
} | |
const ultimateCTAs = document.querySelectorAll("#page-plans-and-pricing .pricingPage__ultimateCta"); | |
if (ultimateCTAs) { | |
ultimateCTAs.forEach(function (cta) { | |
cta.addEventListener("click", function () { | |
optimizelyPush("click_pricing_ultimate_cta"); | |
}); | |
}); | |
} | |
const ultimatePlusCTAs = document.querySelectorAll("#page-plans-and-pricing .pricingPage__ultimatePlusCta"); | |
if (ultimatePlusCTAs) { | |
ultimatePlusCTAs.forEach(function (cta) { | |
cta.addEventListener("click", function () { | |
optimizelyPush("click_pricing_ultimateplus_cta"); | |
}); | |
}); | |
} | |
// Page view events. These depend on logic in mb.module, which populates the drupalSettings.optimizely_custom_events array. | |
if (typeof drupalSettings !== 'undefined' && typeof drupalSettings.optimizely_custom_events !== 'undefined') { | |
let customEvents = drupalSettings.optimizely_custom_events; | |
// Page view - Business Need | |
if (customEvents.includes("page_view_business_need")) { | |
optimizelyPush("page_view_business_need"); | |
} | |
// Page view - Subvertical (a subvertical node that has a parent vertical term). | |
if (customEvents.includes("page_view_subvertical")) { | |
optimizelyPush("page_view_subvertical"); | |
} | |
// Page view - Vertical (a subvertical node that has no parent vertical term). | |
if (customEvents.includes("page_view_vertical")) { | |
optimizelyPush("page_view_vertical"); | |
} | |
} | |
} // End attach. | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment