Send GA4 dataLayer ecommerce events to SourceTrack via GTM

If your site already pushes GA4 ecommerce events (purchase, begin_checkout, generate_lead) to the Google Tag Manager dataLayer, one Custom HTML tag can forward them to SourceTrack. No change to the tracking script is needed, and SourceTrack does not read the dataLayer's ecommerce events on its own: this tag is the bridge.

Agent guidance

  • This recipe uses only the public site key, which is already in the SourceTrack tracking tag. Do not paste a server API token into GTM, into a chat or into any front-end code.
  • Do not invent a value. If the dataLayer event has no transaction_id, value and currency, send nothing for revenue.
  • Do not treat a browser-sent value as confirmed revenue. See "What counts as revenue" below.

Before you start

  1. The SourceTrack tracking tag is already deployed through GTM on All Pages. See the Google Tag Manager guide: https://www.sourcetrack.ai/docs/gtm.
  2. Your site pushes GA4 ecommerce events to the dataLayer in the standard shape, for example:
dataLayer.push({ ecommerce: null });  // clear the previous ecommerce object
dataLayer.push({
  event: 'purchase',
  ecommerce: {
    transaction_id: 'T-12345',
    value: 59.98,
    currency: 'USD',
    items: [{ item_id: 'SKU-1', item_name: 'Example', price: 29.99, quantity: 2 }]
  }
});

What each event becomes in SourceTrack

GA4 event SourceTrack call Counted as
purchase sourcetrack.conversion({ type: 'purchase', order_id, value, currency }) A customer conversion. Its revenue follows the rules below.
generate_lead sourcetrack.conversion({ type: 'lead' }) A lead conversion. No value is sent.
begin_checkout sourcetrack.track('begin_checkout', { cart_value, currency, item_count }) A custom event only. It is not a lead, a customer or revenue.

Set it up in GTM

  1. Variables. Create a Data Layer Variable named DLV - ecommerce with Data Layer Variable Name ecommerce (version 2). Turn on the built-in variable Event (Variables, Configure, Utilities).
  2. Triggers. Create one Custom Event trigger for each event name: purchase, generate_lead, begin_checkout. Or one trigger with the regex match ^(purchase|generate_lead|begin_checkout)$.
  3. Tag. Create a Custom HTML tag, SourceTrack - GA4 events, with this body, and attach the trigger or triggers from step 2:
<script>
(function () {
  var ec = {{DLV - ecommerce}} || {};
  var eventName = {{Event}};

  function send(st) {
    if (eventName === 'purchase') {
      // A purchase needs the id GA4 reports as transaction_id: it is the order_id SourceTrack
      // de-duplicates on, and what a Stripe or Shopify webhook is matched against.
      if (!ec.transaction_id) return;
      var call = { type: 'purchase', order_id: String(ec.transaction_id) };
      var value = Number(ec.value);
      if (isFinite(value) && value > 0 && ec.currency) {
        call.value = value;
        call.currency = String(ec.currency);
      }
      st.conversion(call);
    } else if (eventName === 'generate_lead') {
      st.conversion({ type: 'lead' });
    } else if (eventName === 'begin_checkout') {
      st.track('begin_checkout', {
        cart_value: Number(ec.value) || null,
        currency: ec.currency ? String(ec.currency) : null,
        item_count: Array.isArray(ec.items) ? ec.items.length : null
      });
    }
  }

  // The tracking script loads asynchronously: wait up to 5 seconds for it.
  var tries = 0;
  (function wait() {
    if (window.sourcetrack && typeof window.sourcetrack.conversion === 'function') return send(window.sourcetrack);
    if (++tries < 50) setTimeout(wait, 100);
  })();
})();
</script>
  1. Preview in GTM, trigger a test purchase, then check the Event Debugger on the SourceTrack Setup page (https://app.sourcetrack.ai/setup).

If the site has its own event names, change only the three eventName === comparisons.

What counts as revenue

The tag sends the value your page reports. The SourceTrack rules decide what that value is worth:

  • The browser is not proof of payment. The call uses the public site key, which anyone can read in the page source, so a browser value is never confirmed revenue by itself. A browser conversion is not forwarded to an ad platform until a trusted server-side source has confirmed it.
  • Stripe or Shopify connected. A purchase with a value must carry the order id (the tag sends transaction_id as order_id). It is held until the Stripe or Shopify webhook confirms the same order, and the webhook is the money record. Make transaction_id equal to the order or payment id the processor records; if it does not, the held conversion is not counted as revenue. A purchase with a value and no order id is rejected on these sites, which is why the tag sends nothing without transaction_id.
  • Neither connected. SourceTrack records the value as the page sent it, and it is unverified. Connect Stripe (https://www.sourcetrack.ai/docs/stripe) or Shopify (https://www.sourcetrack.ai/docs/shopify) for server-confirmed revenue.
  • Refunds and negative values are rejected from the browser. Refunds come only from the Stripe or Shopify webhooks.
  • Leads and checkout starts carry no revenue.

Common mistakes

  • Sending the value without transaction_id. The purchase cannot be de-duplicated or matched.
  • Firing the tag on every dataLayer event. Keep the triggers to the three event names above.
  • Sending personal data. The tag sends no email, name or other personal data; do not add any.
  • Counting begin_checkout as a conversion. It is a mid-funnel event and is not reported as one.

Related: the browser conversion call is documented at https://app.sourcetrack.ai/developers/conversions.