BigCommerce has a native GA4 integration that sends e-commerce events directly to Google Analytics 4. However, that requires installing GA4 through BigCommerce, and we also wanted to install GA4 via GTM without tracking duplicate events. BigCommerce only sends e-commerce events to the data layer if the native GA4 integration is being used, so in order to track e-commerce events we’d need to get a little creative.
BigCommerce’s native GA4 integration relies on the BODL, or Big Open Data Layer. While BigCommerce won’t send events to GA4 if we have it installed via GTM, we can still take advantage of the BODL to make our own integration. It works like this:
Tracked ecommerce events for Absolute Lift Parts without using the native GA4 integration, allowing us to place the GA4 tag inside of the GTM container we wanted to install.
<script> function subscribeOnBodlEvents() { // log to verify that the function is executing console.log('run subscribeOnBodlEvents()'); if ( // window is falsy !window || // window.bodlEvents is not defined typeof window.bodlEvents === 'undefined' || // window.bodlEvents.banner is not defined typeof window.bodlEvents.banner === 'undefined' || // window.bodlEvents.checkout is not defined typeof window.bodlEvents.checkout === 'undefined' || // window.bodlEvents.cart is not defined typeof window.bodlEvents.cart === 'undefined' || // window.bodlEvents.product is not defined typeof window.bodlEvents.product === 'undefined' || // window.bodlEvents.consent is not defined typeof window.bodlEvents.consent === 'undefined' ) { // log 'not defined' to the console console.log('not defined'); // and end script execution return; } // view_item if (typeof window.bodlEvents.product.pageViewed === 'function') { // run the pageViewed function to get the payload window.bodlEvents.product.pageViewed(function(payload) { // log the payload to the console console.log('view_item payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'view_item', eventModel: { currency: payload.currency, value: payload.value || 0, // default to 0 if not provided items: payload.line_items.map(function(item){ return { // map each item to the required format item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); }; // view_item_list if (typeof window.bodlEvents.product.categoryViewed === 'function') { // run the categoryViewed function to get the payload window.bodlEvents.product.categoryViewed(function(payload){ // log the payload to the console console.log('view_item_list payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'view_item_list', eventModel: { items: payload.line_items.map(function(item){ return { item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); } // add_to_cart if (typeof window.bodlEvents.cart.addItem === 'function') { // run the addItem function to get the payload window.bodlEvents.cart.addItem(function (payload) { // log the payload to the console console.log('add_to_cart payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'add_to_cart', eventModel: { currency: payload.currency, value: payload.product_value || 0, // default to 0 if not provided items: payload.line_items.map(function(item){ return { item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); } // remove_from_cart if (typeof window.bodlEvents.cart.removeItem === 'function') { // run the removeItem function to get the payload window.bodlEvents.cart.removeItem(function (payload) { // log the payload to the console console.log('remove_from_cart payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'remove_from_cart', eventModel: { currency: payload.currency, value: payload.product_value || 0, // default to 0 if not provided items: payload.line_items.map(function(item){ return { item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); } // view_cart if (typeof window.bodlEvents.cart.viewed === 'function') { // run the viewCart function to get the payload window.bodlEvents.cart.viewed(function(payload) { // log the payload to the console console.log('view_cart payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'view_cart', eventModel: { // use eventModel to encapsulate the payload structure currency: payload.currency, value: payload.cart_value || 0, // default to 0 if not provided items: payload.line_items.map(function(item){ return { item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); } // begin_checkout if (typeof window.bodlEvents.checkout.checkoutBegin === 'function') { // run the checkoutBegin function to get the payload window.bodlEvents.checkout.checkoutBegin(function(payload) { // log the payload to the console console.log('begin_checkout payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'begin_checkout', eventModel: { currency: payload.currency, value: payload.cart_value || 0, // default to 0 if not provided coupon: payload.coupon_codes, items: payload.line_items.map(function(item){ return { item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); } // add_payment_info if (typeof window.bodlEvents.checkout.paymentDetailsProvided === 'function') { // run the paymentDetailsProvided function to get the payload window.bodlEvents.checkout.paymentDetailsProvided(function (payload) { // log the payload to the console console.log('add_payment_info payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'add_payment_info', eventModel: { currency: payload.currency, value: payload.cart_value || 0, // default to 0 if not provided coupon: payload.coupon_codes, // include coupon codes if available payment_type: payload.payment_type, items: payload.line_items.map(function(item){ return { item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); } // add_shipping_info if (typeof window.bodlEvents.checkout.shippingDetailsProvided === 'function') { // run the shippingDetailsProvided function to get the payload window.bodlEvents.checkout.shippingDetailsProvided(function (payload) { // log the payload to the console console.log('add_shipping_info payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'add_shipping_info', eventModel: { currency: payload.currency, value: payload.cart_value || 0, // default to 0 if not provided coupon: payload.coupon_codes, // include coupon codes if available shipping_tier: payload.shipping_method, items: payload.line_items.map(function(item){ return { item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); } // purchase if (typeof window.bodlEvents.checkout.orderPurchased === 'function') { // run the orderPurchased function to get the payload window.bodlEvents.checkout.orderPurchased(function (payload) { // log the payload to the console console.log('purchase payload:', payload); // push to dataLayer window.dataLayer.push({ event: 'purchase', eventModel: { currency: payload.currency, value: payload.cart_value, transaction_id: payload.order_id, coupon: payload.coupon_codes, // include coupon codes if available shipping: payload.shipping_cost, tax: payload.tax, items: payload.line_items.map(function(item){ return { item_id: item.sku, // or item.product_id item_name: item.product_name, price: item.purchase_price || 0, // default to 0 if not provided quantity: item.quantity || 1 // default to 1 if not provided }; }) } }); }); } } // when the page window loads, run the subscribeOnBodlEvents function window.addEventListener('load', subscribeOnBodlEvents, false); </script>