Purchase Event DataLayer Injection WordPress Function
What it does?
-
Fires on order completion: Hooks into the thank-you page and safely loads the order.
-
Pushes a clean event: Sends a
purchaseevent todataLayerwith atransactionIdequal to the order’s display number (string). -
Plays nice with custom numbering: Uses the visible order number, so it aligns with any custom order-number scheme you’ve set.
Why it’s useful?
-
Reliable conversion tracking: Ensures GTM/GA4 receives a consistent purchase signal at the exact right moment.
-
Avoids type pitfalls: Stringifying the ID prevents analytics misreads caused by numeric coercion.
-
Works with custom setups: Ideal when you’re rolling your own GTM events or when other plugins are disabled/insufficient.
Notes
If another plugin already pushes a purchase event, disable one to avoid duplicate conversions.
