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 byWhenWhat
The engineEvery page, firstpage (pageName, as Twig names the page: home, productShow, catShow, cartShow, search, thankyou, …), store, customer (hashed details for a signed-in customer)
The engineA product pagedetail, with the product
The engineThe cart pageThe cart's checkout block (step 1, no event)
The engineThe thank-you pagepurchase, once per order
The engineEach page of products a listing fetchesimpressions
The engineOnce per page loadtwilight::init, then services::<name>.init for each of the store's services (Google Analytics, Google Ads, Clarity, pixels)
The SDKCart and wishlist actionsaddToCart, 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

app/components/product/SizeGuideButton.tsx
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;
PropTypeDescription
enabledbooleantrue when the store has at least one id in settings.keys.gtm.
push(data: Record<string, unknown>) => voidPushes any object to window.dataLayer.
viewPage(pageType: string) => voidPushes { event: 'viewPage', pageType }.
detail(product: Product) => voidProduct page view. Event detail.
impressions(products: Product[], list?: string) => voidProducts shown in a listing, with positions starting at 1. Event impressions. Does nothing for an empty list.
click(product: Product, list?: string, position?: number) => voidProduct clicked in a listing. Event click.
add(product: Product, quantity?: number) => voidAdded to the cart. Event addToCart. Quantity defaults to 1.
remove(item: CartItem) => voidRemoved from the cart. Event removeFromCart.
checkout(cart: Cart, step?: number) => voidCheckout started. Event checkout. Step defaults to 1.
purchase(orderId: string | number, cart: Cart) => voidOrder 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, click and add is the product's own currency. For remove, checkout and purchase it is the signed-in customer's preferred currency, and SAR for guests.
  • The hook calls useUser inside, so the result is a new object on every render. Do not use gtm as a useEffect dependency; keep it in a ref, as in the page views recipe.
  • detail, impressions, add, remove, checkout and purchase send 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::*.init with a page.pageName check, fires for the first page only. Trigger such tags on the named events (detail, impressions, addToCart, …), which every page sends.

Next

On this page