Tutorial · GTM · 14 min

    GTM dataLayer Patterns That Survive a Replatform

    Object-shaped events, normalized values and a per-page snapshot. Patterns that don't break when the engineering team rewrites the frontend.

    1. Step 01

      Use object-shaped events

      Flat dataLayer keys become unmanageable past a dozen events. Nest them.

      dataLayer.push({
        event: 'transaction_completed',
        transaction: {
          id: 'txn_123',
          amount: 1200,
          currency: 'BDT',
          type: 'p2p',
          sender_id_hash: 'sha256:...',
          recipient_id_hash: 'sha256:...'
        }
      });

      In GTM, read with a single Data Layer Variable transaction and project sub-fields with Custom JavaScript variables.

    2. Step 02

      Push a page snapshot on every route

      SPAs need an explicit page_view push on every route change. Don't rely on the History Change trigger alone — it fires before your route component has populated dataLayer.

      // In your router's onRouteChange:
      dataLayer.push({
        event: 'page_view',
        page: {
          path: location.pathname,
          title: document.title,
          section: deriveSection(location.pathname)
        }
      });
    3. Step 03

      Normalize values

      Send amounts in the minor unit (cents/paisa) and the currency code separately. Future-you will thank present-you when you start reporting in multiple currencies.

    4. Step 04

      Reset on logout, not on push

      Don't dataLayer = [] mid-session — GTM will lose its history. On logout, reset only user-scoped keys: dataLayer.push({ user: null }).

    5. Step 05

      Hash PII before it ever reaches dataLayer

      Phone numbers, emails, national IDs: hash with SHA-256 in your application code, push the hash. This is non-negotiable in regulated environments.

    6. Step 06

      Version your schema

      Add schema_version: 2 to every event. When you change a field, bump the version and keep both readers in GTM for a quarter. This is how you replatform without losing reporting continuity.