# Google Tag Manager recipe for embeds

This recipe brings the [events of your embeds](/help/booking-pages/embed-events) into Google Tag Manager on your
website, and sends them on to Google Analytics 4, Google Ads and the Meta Pixel with your own tags.

The embed sends its events whatever the visitor chose on your website, because consent for the tags on your
website is yours to collect. Give each tag below a consent condition, as [step 7](#7-consent-for-your-tags) shows.

## 1. Get the events into the data layer

Pick the way that matches your embed. Don't use both on one page, or every event arrives twice.

**With the 42min embed code.** In the embed panel, set **Send events to** to **Google Tag Manager** and paste
the new code on your page. Each event then arrives in the data layer as two pushes, the first one clearing the
previous event's values:

```js
{ x42min: null, x42min_identifiers: null }
{ event: 'x42min_booking_confirmed', x42min: { event_type_name: 'Intro call', booking_id: '8f0c2a8e-1d4b-4b8e-9a51-0e6f3c1a2b7d' }, x42min_identifiers: null }
```

**With a plain iframe, or inside a site builder.** If your page still has an `<iframe>` code from before, or a
builder such as Wix or Framer puts your code in a frame of its own, add a **Custom HTML** tag that fires on
**Initialization - All Pages**:

```html
<script>
  (function () {
    window.addEventListener('message', function (event) {
      if (event.origin !== 'https://42min.us') return;
      var data = event.data;
      if (!data || data.type !== 'x42min:event' || data.version !== 1) return;
      window.dataLayer = window.dataLayer || [];
      window.dataLayer.push({ x42min: null, x42min_identifiers: null });
      window.dataLayer.push({ event: data.name, x42min: data.params, x42min_identifiers: data.identifiers || null });
    });
  })();
</script>
```

With a plain iframe or inside a site builder, the messages never carry hashed identifiers, because the embed
script that tells the frame your website's address isn't on the page.

## 2. Variables

Create a **Data Layer Variable** (version 2) for each value you need:

| Variable name | Data Layer Variable Name |
|---|---|
| `DLV x42min event_type_name` | `x42min.event_type_name` |
| `DLV x42min routing_form_name` | `x42min.routing_form_name` |
| `DLV x42min booking_id` | `x42min.booking_id` |
| `DLV x42min value` | `x42min.value` |
| `DLV x42min currency` | `x42min.currency` |
| `DLV x42min identifiers google` | `x42min_identifiers.google` |
| `DLV x42min identifiers meta` | `x42min_identifiers.meta` |

The other parameters are listed on [Events from embeds](/help/booking-pages/embed-events#parameters).

## 3. Triggers

- **x42min events**: a **Custom Event** trigger with the event name `^x42min_` and **Use regex matching** on.
- **x42min booking confirmed**: a **Custom Event** trigger with the event name `x42min_booking_confirmed`.

## 4. Google Analytics 4

Add a **Google Analytics: GA4 Event** tag with your Measurement ID, the event name `{{Event}}`, and an event
parameter for each variable you want in your reports, for example `event_type_name` set to
`{{DLV x42min event_type_name}}`. Fire it on **x42min events**, and require `analytics_storage` for it, as
[step 7](#7-consent-for-your-tags) shows.

## 5. Google Ads

Add a **Google Ads Conversion Tracking** tag with your Conversion ID and the conversion label, and the
transaction ID `{{DLV x42min booking_id}}`. Fire it on **x42min booking confirmed**, and require `ad_storage` and
`ad_user_data` for it ([step 7](#7-consent-for-your-tags)).

**Enhanced conversions, in Google Ads first.** Under **Goals**, open **Settings**. Expand **Customer data use**,
check **Turn on enhanced conversions**, and confirm that the Google Ads Data Processing Terms apply. An account
that had already agreed to Google's customer data terms was switched over automatically, so check that the box is
on. Google no longer asks you to choose an implementation method. Until enhanced conversions are on, Google
doesn't use the hashed details that reach the tag.

**Then in the tag.** In the Google Ads Conversion Tracking tag above, check **Include user-provided data from your
website**. Create a **User-Provided Data** variable, choose **Code**, and pick `{{DLV x42min identifiers google}}`,
the variable that maps `x42min_identifiers.google`. Its keys, `sha256_email_address` and `sha256_phone_number`,
are the names Google uses for data hashed in advance. The identifiers are there only when your website is listed
and Enhanced conversions is on: see [Hashed email and phone](/help/booking-pages/embed-events#hashed-email-and-phone).

Use the variable on this conversion tag alone. Don't add it to your GA4 configuration or to any Google tag that
sends to Google Analytics, or the hashed details would travel with events that aren't conversions. Check once in
Tag Assistant that the user-provided data shows on the conversion tag and on no other tag.

## 6. Meta Pixel

With your pixel's base code firing first, add a **Custom HTML** tag fired on **x42min booking confirmed**. A
Custom HTML tag has no consent check of its own, so require `ad_storage` and `ad_user_data` for it
([step 7](#7-consent-for-your-tags)):

```html
<script>
  var userData = {{DLV x42min identifiers meta}};
  if (userData) fbq('init', 'YOUR_PIXEL_ID', userData);
  fbq('track', 'Schedule', { content_name: {{DLV x42min event_type_name}} }, { eventID: {{DLV x42min booking_id}} });
</script>
```

`{{DLV x42min identifiers meta}}` maps `x42min_identifiers.meta`: the hashed `em` and `ph` for Meta's advanced
matching, there only when your website is listed and Advanced matching is on. Passing them to `fbq('init', ...)`
with your pixel's ID is the step the embed code leaves to you, because only your tags know that ID. Replace
`YOUR_PIXEL_ID` with your pixel's ID, and `Schedule` with the standard event you count.

## 7. Consent for your tags

Google Tag Manager fires a tag on its trigger unless you give the tag a consent condition. In each tag from steps
4 to 6, open **Advanced Settings**, then **Consent Settings**. Under **Additional Consent Checks**, choose
**Require additional consent for tag to fire**, click **Add required consent**, and add:

- `analytics_storage` for the Google Analytics 4 event tag;
- `ad_storage` and `ad_user_data` for the Google Ads Conversion Tracking tag and for the Meta Custom HTML tag.

These checks work when your website's consent setup, a consent management platform or Google's consent mode,
tells Google Tag Manager what the visitor chose. If your consent platform offers its own trigger condition or
blocking trigger instead, use that.

## 8. Check it

Open your page in Tag Assistant (**Preview** in Google Tag Manager), make a test booking through the embed, and
check that each `x42min_` event appears in the data layer and fires the tags you expect.

Then test the other states. Decline tracking in your own consent banner, make another test booking, and check that
the three tags don't fire. Do the same after withdrawing a consent you gave earlier.
