Files
ipal-kit/docs/gtm-meta-pixel.md
T

198 lines
8.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# GTM + Meta Pixel — setup (ipal-kit stack)
A step-by-step guide to wiring Meta Pixel through Google Tag Manager on a
site built on `@intecion/ipal-kit`. Written for our stack: the plugin already does part
of the work, the rest you click in the GTM panel — in the project code only a
**one-time** wiring of the form analytics fields (step 0.3), then zero code per
form.
Related: [analytics.md](./analytics.md), [consent.md](./consent.md),
[forms.md](./forms.md).
---
## What the plugin does FOR you (you do not configure this in code)
- **Loads the GTM container** — `Analytics` takes the `GTM Container ID` from the panel
(Settings → Site integrations). Zero code.
- **Consent Mode v2** — before loading the container it sends `consent default`
(the saved decision or everything `denied`), and after a click in the banner `consent
update`. The GTM container itself loads **before consent** (advanced mode) — that is why
the consent condition in every Meta tag (step 7) is mandatory. The
**marketing** category → `ad_storage` / `ad_user_data` / `ad_personalization`.
- **The `form_submit` event** — after a successful form submission, with parameters
set **in the panel** (the "Analytics (GTM / dataLayer)" field group on the
form — see below). Needs a one-time
wiring of `buildFormAnalyticsFields()` + `formAnalyticsOnSuccess` (step 0.3).
What is left: wire the form analytics fields once, enter the ID in the panel, configure
the tags in GTM, set the event on the form in the panel. Leave the **Meta Pixel ID** field in Site
Integrations empty (otherwise the plugin loads a second pixel → double counting).
---
## 0. Prerequisites (Payload panel)
1. **GTM Container ID** in Settings → Site integrations (`GTM-XXXXXXX`).
2. **Cookie banner** active (`CookieBanner` + `ConsentProvider`) — otherwise
Consent Mode gets no decision.
3. **Analytics field on the form** — add `buildFormAnalyticsFields()` to the
Forms collection (see [forms.md](./forms.md) → "Managing from the PANEL"), and in the
renderer: `onSuccess: formAnalyticsOnSuccess(form.analytics)`. In the panel,
on the contact form, in the **Analytics (GTM / dataLayer)** group
set: **Event name** `form_submit` (the default value; empty = no event),
in **Event parameters** `key = form_name`, `value = $topic` (`$` = the value of a
form field; or a constant, e.g. `kontakt`).
---
## 1. Pixel ID as a GTM variable
Events Manager → Data Sources → your pixel → copy the **Pixel ID** (15–16 digits).
GTM → Variables → User-Defined Variables → **New** → **Constant** →
value = Pixel ID → name it `Meta Pixel ID`.
## 2. Meta tag template
Meta has no native tag in GTM. Two ways:
- **Community template (easier):** Templates → Tag Templates → Search
Gallery → "Facebook Pixel" → Add to workspace.
- **Custom HTML:** paste the pixel base code from Events Manager (the option for full
control; then skip the template and build the tags as Custom HTML).
Below I assume the template.
## 3. Base tag — PageView
Tags → **New** → type **Facebook Pixel**:
- **Pixel ID:** `{{Meta Pixel ID}}`
- **Event:** Standard → `PageView`
- **Consent Settings** (Advanced → Consent Settings): require **`ad_storage`**.
- **Trigger:** `All Pages` + (for Next.js) a second **History Change** trigger —
see the SPA section below.
Name it e.g. `Meta – Base + PageView`.
## 4. Variable for the form parameter
Variables → **New** → **Data Layer Variable**:
- Data Layer Variable Name: `form_name`
- Version: Version 2
- At the top, name it exactly `DLV – form_name`.
> The name in the tag and in the variable must be **character for character** the same, including the
> dash. If the tag has an en dash "–" and you type a plain
> "-" in the variable, GTM treats them as two different variables. The safest way is to copy the text between
> `{{` and `}}`.
## 5. Trigger — form submission
Triggers → **New** → **Custom Event**:
- Event name: `form_submit` (exactly like the event from the form panel)
- This trigger fires on: All Custom Events.
Name it `CE – form_submit`.
## 6. Conversion tag — Lead
Tags → **New** → **Facebook Pixel**:
- **Pixel ID:** `{{Meta Pixel ID}}`
- **Event:** Standard → `Lead`
- **Object Properties / parameters:** `content_name` = `{{DLV – form_name}}`
- **Consent Settings:** require **`ad_storage`** (same as the base tag).
- **Trigger:** `CE – form_submit`.
Name it `Meta – Lead`.
Other conversions work the same way, each as a separate tag from the same template:
- **Contact** — trigger on a click on `tel:` / `mailto:`.
- **ViewContent / AddToCart / Purchase** — parameters (`value`, `currency`,
`content_ids`) from data layer variables; the event is pushed from code/panel
where the action happens.
---
## 7. Consent (GDPR) — critical
A marketing pixel **must not fire before consent**. In every Meta tag set
Consent Settings → require `ad_storage`. The plugin keeps `ad_storage = denied` until
the banner is accepted and sends `update` after the click — only then does the tag
fire. Without this condition the pixel fires without consent (a GDPR violation).
In Preview check: **before** accepting the banner → the `Meta – Base + PageView` tag
must be **blocked**; **after** accepting → "Fired".
## 8. Next.js / SPA — PageView on route change
With client-side navigation (Next.js routing without a full reload)
`All Pages` fires only once. Two options:
- **In GTM (no code, recommended):** add a second trigger of type
**History Change** to the PageView tag. GTM catches the route change.
- **From code (if you prefer explicit events):** push `page_view` to the dataLayer on
route change (`pushDataLayer('page_view')` on a `usePathname` change) and create
a `CE – page_view` trigger.
In Preview / Events Manager count: there must be **one** PageView per page
change and no double count of the first visit.
## 9. Duplicates
If the pixel is also hard-coded in the site code or added through another plugin
— **remove it from there**. It must live only in GTM, otherwise events are counted twice.
---
## 10. Test
1. **GTM Preview** (top right corner) → enter the site address.
2. Accept marketing cookies.
3. Tag Assistant: `Meta – Base + PageView` = "Fired" (blocked before
consent).
4. Submit the form → the `form_submit` event in the left column, next to it the
`Meta – Lead` tag "Fired"; in the Variables tab `DLV – form_name` has a value
(e.g. `kontakt`).
5. **Meta Pixel Helper** (Chrome extension) → sees PageView and Lead.
6. **Events Manager → Test Events** → enter the address → `PageView` and, after
submitting the form, `Lead` with the `content_name` parameter.
## 11. Publishing
GTM → **Submit** → version name (e.g. "Meta Pixel – PageView + Lead") →
**Publish**. Until then nothing works in production — saving the tag alone is not
enough.
---
## 12. (Optional) Conversions API (CAPI)
To send events from the server too (resilience against blockers/iOS), each
event needs a shared `event_id` in the pixel and in CAPI so Meta can
deduplicate them. That is a separate topic (server-side GTM or sending from the backend) — in this
stack `submitForm` (server) could generate the `event_id`, and `pushDataLayer`
would use the same one. If you move to CAPI, report it — it is an extension of the
forms + analytics modules, not GTM configuration.
---
## Cheat sheet — who does what
| Element | Where |
|---|---|
| Loading GTM, Consent Mode | plugin (auto) |
| `form_submit` event + parameters | Payload panel ("Analytics" group on the form) |
| Pixel ID, tags, triggers, consent conditions | GTM panel |
| Publishing the container | GTM panel |
| Project code | nothing (apart from the one-time `buildFormAnalyticsFields()` in Forms and `onSuccess: formAnalyticsOnSuccess(...)` in the renderer) |
## Turn off, override, extend
- **Turn off:** empty *GTM Container ID* in Site integrations = the container does not load; empty
*Event name* on the form = no `form_submit`.
- **Override:** event name and parameters per form in the panel; instead of `formAnalyticsOnSuccess`
you can call `pushDataLayer(event, params)` in `onSuccess` with your own data.
- **Extend:** any other events → `pushDataLayer` (`/client`); tags, triggers and consent
conditions — in GTM.