Sharing some code which helps me to trigger a re-direct and custom event in GTM after a successful booking.
Hope it helps someone and please let me know if you see any issues in the coding.
Thanks!
+++
window.ameliaActions = { beforeBooking: function (success = null, error = null, data) { // console.log('Before booking is created HOOK'); // console.log(data); // console.log('------------');
// Determine GTM event name based on Amelia Service name and redirect URL based on the name const name = data.componentProps.bookable.name; let gtmEventName = ''; let redirectUrl = '';
// Trigger custom event for GTM with debugging if (window.dataLayer && gtmEventName) { console.log(`Pushing event to GTM: ${gtmEventName}`); window.dataLayer.push({ event: gtmEventName, bookingData: data }); } else { console.error('GTM event not pushed: dataLayer or gtmEventName is missing'); }
// Redirect if URL is determined if (redirectUrl) { // Call success callback for the booking to be completed success(); window.location.href = redirectUrl; } else { // Call success callback for the booking to be completed success(); } } }
Sharing some code which helps me to trigger a re-direct and custom event in GTM after a successful booking.
Hope it helps someone and please let me know if you see any issues in the coding.
Thanks!
+++
window.ameliaActions = {
beforeBooking: function (success = null, error = null, data) {
// console.log('Before booking is created HOOK');
// console.log(data);
// console.log('------------');
// Determine GTM event name based on Amelia Service name and redirect URL based on the name
const name = data.componentProps.bookable.name;
let gtmEventName = '';
let redirectUrl = '';
if (name === 'Coaching Call - Individual') {
gtmEventName = 'bookingCompletedIndividual';
redirectUrl = '/thank-you-individual';
} else if (name === 'Coaching Call - Group') {
gtmEventName = 'bookingCompletedGroup';
redirectUrl = '/thank-you-group';
}
// Trigger custom event for GTM with debugging
if (window.dataLayer && gtmEventName) {
console.log(`Pushing event to GTM: ${gtmEventName}`);
window.dataLayer.push({
event: gtmEventName,
bookingData: data
});
} else {
console.error('GTM event not pushed: dataLayer or gtmEventName is missing');
}
// Redirect if URL is determined
if (redirectUrl) {
// Call success callback for the booking to be completed
success();
window.location.href = redirectUrl;
} else {
// Call success callback for the booking to be completed
success();
}
}
}
Hello Ivan,
Thank you very much for sharing that with other people.
Hope that this will be useful for a lot of Amelia users.
Kind Regards,
Uros Jovanovic
[email protected]
Rate my support
wpDataTables: FAQ | Facebook | Twitter | Instagram | Front-end and back-end demo | Docs
Amelia: FAQ | Facebook | Twitter | Instagram | Amelia demo sites | Docs | Discord Community
You can try wpDataTables add-ons before purchasing on these sandbox sites:
Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables