useGtm
Push Google Tag Manager events of your own to the data layer. The engine already pushes what a Twig page does.
Use useGtm when your theme has an event of its own to report to the merchant's Google Tag Manager containers, such as a size guide being opened.
You don't need it for the store's standard data. The engine fills the data layer the way a Twig page does, so Salla's container and the merchant's own work unchanged:
| Pushed by | When | What |
|---|---|---|
| The engine | Every page, first | page (pageName, as Twig names the page: home, productShow, catShow, cartShow, search, thankyou, …), store, customer (hashed details for a signed-in customer) |
| The engine | A product page | detail, with the product |
| The engine | The cart page | The cart's checkout block (step 1, no event) |
| The engine | The thank-you page | purchase, once per order |
| The engine | Each page of products a listing fetches | impressions |
| The engine | Once per page load | twilight::init, then services::<name>.init for each of the store's services (Google Analytics, Google Ads, Clarity, pixels) |
| The SDK | Cart and wishlist actions | addToCart, removedFromCart, wishlist::added, and every SDK event by name |
The engine loads every container in settings.keys.gtm, after the first page's data is in.
Example
import { useGtm } from '@salla.sa/twilight-theme-engine/hooks/useGtm';
import type { Product } from '@salla.sa/twilight-theme-engine/types';
export function SizeGuideButton({ product, onOpen }: { product: Product; onOpen: () => void }) {
const gtm = useGtm();
return (
<button
onClick={() => {
gtm.push({ event: 'size_guide_opened', product_id: product.id });
onOpen();
}}
>
Size guide
</button>
);
}API
function useGtm(): UseGtmResult;| Prop | Type | Description |
|---|---|---|
enabled | boolean | true when the store has at least one id in settings.keys.gtm. |
push | (data: Record<string, unknown>) => void | Pushes any object to window.dataLayer. |
viewPage | (pageType: string) => void | Pushes { event: 'viewPage', pageType }. |
detail | (product: Product) => void | Product page view. Event detail. |
impressions | (products: Product[], list?: string) => void | Products shown in a listing, with positions starting at 1. Event impressions. Does nothing for an empty list. |
click | (product: Product, list?: string, position?: number) => void | Product clicked in a listing. Event click. |
add | (product: Product, quantity?: number) => void | Added to the cart. Event addToCart. Quantity defaults to 1. |
remove | (item: CartItem) => void | Removed from the cart. Event removeFromCart. |
checkout | (cart: Cart, step?: number) => void | Checkout started. Event checkout. Step defaults to 1. |
purchase | (orderId: string | number, cart: Cart) => void | Order placed. Event purchase, with revenue, tax and coupon from the cart. |
Ecommerce events carry an ecommerce object with currencyCode, a unique event_id, and the products (id, name, price, brand, category, quantity). Product price is sale_price.
Gotchas
- Everything is a no-op when the store has no Tag Manager id, so you can call the methods without checking
enabled. - The engine loads the Tag Manager script itself. This hook only pushes events to
window.dataLayer. - Call the methods from effects and event handlers, not during render. They use
window, so they only work in the browser. - The currency of
detail,impressions,clickandaddis the product's owncurrency. Forremove,checkoutandpurchaseit is the signed-in customer's preferred currency, andSARfor guests. - The hook calls
useUserinside, so the result is a new object on every render. Do not usegtmas auseEffectdependency; keep it in a ref, as in the page views recipe. detail,impressions,add,remove,checkoutandpurchasesend what the engine or the SDK already sends (table above), in a different shape. Calling them as well counts each event twice.- A page reached by a link loads nothing, so a container tag that fires on DOM Ready, or on
services::*.initwith apage.pageNamecheck, fires for the first page only. Trigger such tags on the named events (detail,impressions,addToCart, …), which every page sends.
Next
- Track page views: send
viewPageon every client-side navigation. - Store data:
keys.gtm. - Cart data: the
CartandCartItemyou pass tocheckoutandpurchase.