Google Tag Manager
Send your campaign's events to your own Google Tag Manager container, and build tags on them.
Google Tag Manager (GTM) does not report anything by itself. It is a container for your own tags. Your campaign sends its events to GTM, and the tags you build decide which platforms hear about them.
Use GTM when you want to manage tags yourself, or for a platform that has no guide in this section.
Before you start
- A GTM web container and its ID, which looks like
GTM-XXXXXXX. - A campaign that is ready for tracking. See Set up tracking.
The Google Tag Manager app you set up under Apps in your NEXT dashboard only adds its script to your Storefront. It is not added to your Campaigns, so follow this page to add GTM to your campaign.
This page is for campaigns on Campaign Cart SDK 0.5.2, the version the starter templates load. Where a campaign still on SDK 0.4.x behaves differently, the page says so. The SDK version is in your campaign's _data/campaigns.json as sdk_version.
Set up
Setup has two parts, and you need both.
- In
_data/campaigns.json, add your container ID to your campaign. This loads your container on every page:
{
"my-campaign": {
"name": "My Campaign",
"gtm_id": "GTM-XXXXXXX"
}
}- In
assets/config.js, switchgtmon. This makes the campaign send its events to the container:
// Inside analytics.providers:
gtm: {
enabled: true
},- Build with
npm run buildand deploy.
Both parts are needed
With only gtm_id, the container loads but receives no campaign events. With only the config.js setting, the events are sent but there is no container to receive them.
SDK 0.5.2 also reads a newer setting for the same switch, analytics.destinations.gtm.enabled. You only need one of them. If you set both, the destinations one is used.
Events you can build tags on
Each event reaches GTM as a data layer event. Create a Custom Event trigger with the event name, then attach your tag.
| Event name | When it happens |
|---|---|
dl_user_data | Any page loads. Use this for page view tags |
dl_view_item | A page shows a product |
dl_add_to_cart | An item is added to the cart |
dl_begin_checkout | The checkout loads |
dl_add_shipping_info | A shipping address is entered |
dl_add_payment_info | Payment details are entered |
dl_purchase | The order is paid. See Purchases and upsells for which page sends it |
dl_viewed_upsell | An upsell page is shown |
dl_upsell_purchase | An upsell is accepted. Sent on the page after it |
SDK 0.5.2 also sends dl_generate_lead when the shopper's contact details are saved at checkout, and dl_skipped_upsell when an upsell is declined.
These dl_* events use the same format as SDK 0.4.x, so tags built for a 0.4.x campaign keep working on 0.5.2. The SDK now calls this format deprecated, but it still sends it.
The full list, with every field, is in the SDK's GTM destination reference. For a campaign on SDK 0.4.x, use the 0.4.43 analytics events reference.
Reading values in your tags
Create Data Layer Variables in GTM for the values you need.
| What you want | Data layer variable |
|---|---|
| Order number | ecommerce.transaction_id |
| Amount | ecommerce.value |
| Currency | ecommerce.currency |
| Items | ecommerce.items |
| Shipping and tax | ecommerce.shipping, ecommerce.tax |
| UTM parameters | attribution.utm_source, attribution.utm_medium, attribution.utm_campaign |
| Google click ID | attribution.gclid |
| Meta click ID | attribution.metadata.fbclid |
The events also carry the shopper's details under user_properties. dl_purchase and dl_upsell_purchase carry the email, name, phone and billing address. On SDK 0.5.2 the events before the purchase carry only the email and country once the shopper has typed them, and dl_user_data also carries the name and phone. Decide which tags may read them, and see Privacy and consent.
Purchases and upsells
dl_purchase | dl_upsell_purchase | |
|---|---|---|
| When, SDK 0.5.2 | The checkout page, as soon as the payment goes through. If the shopper pays away from the page, for example with PayPal, the first campaign page they return to | The page after an upsell is accepted |
| When, SDK 0.4.x | The first page after payment | The page after an upsell is accepted |
| Order ID | The order number, for example 100041 | The order reference followed by -US1, -US2 and so on |
| Amount | The price of the items as shown on your store, without shipping | The upsell only |
- A purchase is sent once per order. Reloading the page does not send it again.
- On SDK 0.5.2, if the shopper leaves the checkout before the purchase is sent, the next campaign page they open sends it.
- A tag on
dl_purchasealone misses upsell revenue. Add a tag ondl_upsell_purchaseif you want it. - The amount is the item prices only. Add
ecommerce.shippingif your platform should see shipping. On a store that shows prices without tax, addecommerce.taxas well.
Send each platform one purchase
If a platform is already turned on in your campaign, for example GA4 or TikTok, do not also send it purchases from GTM. See Send each purchase from one place.
Google Ads conversions
To count purchases in Google Ads from GTM, add a Google Ads conversion tag on a dl_purchase trigger. Set its order ID to ecommerce.transaction_id, its value to ecommerce.value and its currency to ecommerce.currency. Add a second tag on dl_upsell_purchase to count upsells.
Do this only if you are not already importing purchases into Google Ads from GA4. See Use GA4 purchases in Google Ads.
Keep an event out of GTM
To stop one event reaching your container, list it in blockedEvents in assets/config.js. There are two ways to write it:
// Inside analytics.providers, with dl_* names. Works on SDK 0.4.x and 0.5.2:
gtm: {
enabled: true,
blockedEvents: ['dl_upsell_purchase']
},// Inside analytics, with the SDK's own event names. SDK 0.5.2 only:
destinations: {
gtm: { blockedEvents: ['upsell_completed'] }
},Use one or the other. On 0.5.2, a destinations.gtm.blockedEvents list replaces the providers.gtm.blockedEvents list; the two are not combined. The names that match each dl_* event are in the GTM destination reference.
A blocked event is kept off window.dataLayer, which is what GTM reads. It still reaches window.NextDataLayer, so a script that reads that one still sees it.
Verify it works
- Start Preview mode in your GTM container and open your campaign.
- You should see
dl_user_datafirst, then the other events as you move through the funnel. - Place an order with a test card.
dl_purchaseappears with the order number: on SDK 0.5.2 while you are still on the checkout page, on SDK 0.4.x on the next page. - Accept an upsell.
dl_upsell_purchaseappears on the page after it.
On SDK 0.5.2 you can also open any campaign page with ?debug=1 at the end of the link. The SDK's debug panel opens, and its Analytics tab lists each event the page sent, whether it reached GTM, and why not if it did not. Open a page with ?debug=0 to close the panel again. See See what was sent.
Troubleshooting
| Problem | Likely cause | Fix |
|---|---|---|
| The container does not load | gtm_id is missing or empty | Add your container ID and rebuild |
| The container loads but receives no events | gtm is switched off in assets/config.js | Switch it on |
| One event never reaches the container | It is listed in blockedEvents | Remove it from the list. See Keep an event out of GTM |
| Tags do not fire | The trigger's event name does not match | Copy the name from GTM Preview |
| A page view trigger never fires | It listens for dl_page_view | Use dl_user_data |
More help is in Test and troubleshoot tracking.
Hand-built campaigns
On a campaign that is not built from a starter template, add your GTM container to every page yourself.
- SDK 0.5.2: the SDK sends its
dl_*events towindow.dataLayerunless your page turns them off, so the container is all you add. The starter templates turn them off inassets/config.jsuntil you switchgtmon, which is why step 2 is needed there. See the GTM destination reference. - SDK 0.4.x: switch
gtmon as in step 2. See the 0.4.43 analytics events reference.
On SDK 0.5.2, developers can also listen to the SDK's own events with next.analytics.on and push them to the container in whatever shape the tags need. Turn the dl_* events off if you do, or the container gets each event twice. See Destinations.