Because the LokiCheckout uses a JavaScript implemementation that is hugely different from other checkouts, JavaScript integrations like one with Google Tag Manager need to be recreated. This document describes the possible implementation details for Google Tag Manager in the Loki Checkout.
The following GA4 events (and others) are possible:
begin_checkout
add_shipping_info
add_payment_info
purchase
view_item_list
view_cart
When integrating GTM into the LokiCheckout, you need to be aware of the fact that the actual quote data lives on the Magento server, while the JavaScript data is just a reflection of it. How the JavaScript side is updated with the right data, is depending on things. Common data sources here are:
window.checkoutConfig
- a static configuration which should only be used for reading. Note that the Loki Checkout rarely depends on this, because all Loki Components directly read from the server at rendering time;/customer/section/load
and localStorage;The first event begin_checkout
is easiest to deal with. Once the Loki Checkout page is loaded, the checkout begins:
(function() {
// trigger begin_checkout
})();
The GA4 begin_checkout
event requires some data - currency, value, coupon, items - so the next question is how to retrieve data.
For this, the LokiCheckout has added its own private data section loki-checkout
to the AJAX endpoint /customer/section/load
which is automatically synced to localStorage. Access to this private data is streamlined with an Alpine.js store LokiLocalStorage
which makes all data within that store reactive. If you build your own Alpine.js component, properties retrieved from the Alpine.store store automatically become reactive properties in your component as well. If you are not building your own component, you need to make your JavaScript code reactive by wrapping it within Alpine.effect(() => {}
:
document.addEventListener('alpine:init', () => {
Alpine.effect(() => {
const lokiCheckoutSection = Alpine.store('LokiLocalStorage').get('loki-checkout');
console.log('lokiCheckoutSection', lokiCheckoutSection);
// trigger begin_checkout
});
});
The data in the LokiLocalStorage
store is kept up-to-date during component updates via a special Loki Component with block name loki-checkout.utils.private-data
. This component is a target of any other component, meaning that any component update will trigger a refresh of this component as well. Once this specific component refreshes, it copies its own data (the same as retrieved via an AJAX call) towards local storage.
In short, the localStorage section loki-checkout
is kept up-to-date at all times!
If there are no component updates, in a regular situation, the local storage will also be up-to-date as well, unless some third party script makes its own AJAX calls. In that case, if you want to refresh the data manually (by making a new AJAX call) you can do this as follows:
document.addEventListener('alpine:init', () => {
Alpine.store('LokiLocalStorage').refresh('loki-checkout');
});
purchase
eventWe recommend the purchase
event to be triggered on the success-page, which places it outside of the scope of the LokiCheckout.