# Google Tag Manager (GTM)

## Configuring Triggers

Combining Google's tags and triggers has many different uses, from inserting custom JavaScript code to sending information to Google Analytics, or even forwarding information to integrations outside what Formsort supports!&#x20;

A **trigger** is only one part of the equation - it detects *when* something should happen. A **tag** is the other half of the equation - it tells *what* should happen when the trigger fires, e.g. insert custom JavaScript, or send data to Google Analytics.&#x20;

**Configuring Triggers** is going to:&#x20;

* first, get your GTM container connected to Formsort so event data from your Flows can be sent to GTM;
* then, show you how to create a GTM trigger, and check that the trigger fires on Formsort's events.

We'll do this by having a GTM trigger on Formsort's **FlowLoaded** event, which will create an alert on your browser page that lets us know our Google Tag is firing when the flow is loaded.&#x20;

This process should give you enough familiarity that you can start creating triggers for the tags you will later create to capture data.&#x20;

#### Grab your container ID from GTM&#x20;

Find your GTM container ID. It will start with "GTM-".

![GTM container ID ](https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FDW483LmIEDfP5Q6e9gqx%2Fgtm-image-1.png?alt=media\&token=099bf5a2-2fdb-46ea-b46a-2402c6f47c8f)

#### Configure sending events in Formsort&#x20;

In your Flow's Google Tag Manager [Integrations](https://docs.formsort.com/integrations/integration-reference) tab, add your container ID. Enable any events you want to be available for GTM to detect.&#x20;

{% hint style="info" %}
These events can be used to set up the GTM Trigger. Continue reading to see how that works!&#x20;
{% endhint %}

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FX2m2vFSEeFUuLeIMva8p%2Fimage.png?alt=media&#x26;token=0f623a48-98e6-4d6d-860c-a8c5feb2f011" alt=""><figcaption><p>Once container ID is added, events can be enabled</p></figcaption></figure>

Note that as long as a Container ID is provided, GTM will always be loaded, and the **Enabled Events** menu will be available. The [analytics events](https://docs.formsort.com/integrations/getting-data-out/analytics) you choose will be available for Google Tag Manager to pick up on (i.e. FlowLoaded, FlowClosed, etc).

The **enabled events** options control the events within Formsort that send `dataLayer.push` variables (`stepId`, `variantLabel`, etc).&#x20;

Make sure to:

* Choose at least **FlowLoaded** under enabled events.
* Save your changes.

{% hint style="info" %}
**FlowLoaded** is chosen for the purposes of this guide. In reality, you can choose any custom event you require (StepLoaded, FlowFinalized, etc.)
{% endhint %}

The [shape of the payload](https://docs.formsort.com/integrations/getting-data-out/event-payload-shape) will also be configured and can be seen in "preview event type" at the bottom of the menu. Choosing to **Send answers** will allow you to push answers to GTM as well.

**Append StepID** **to StepLoaded and StepCompleted** **event names** will add the [Step ID](https://docs.formsort.com/building-flows/adding-content/steps/settings) to the payload when you receive a StepLoaded and StepCompleted event.

If **Send answers** is enabled, an answers payload will be present in the `dataLayer.push()` object that is sent with your events. More about that will be covered in ["Previewing" GTM tags in GA4](#previewing-gtm-tags-in-ga4).

{% hint style="warning" %}
Once you've finished enabling or disabling your events in this integration menu, head over to the variants within the flow that you want to pick up these changes. Deploy or re-deploy these variants. **Changes in the Integration menu are not automatically deployed to variants**, so it is imperative that you do this yourself.
{% endhint %}

#### Configure the triggers and tags in GTM&#x20;

Now, head back to your GTM container.&#x20;

Under **Trigger Type**, use a `Custom Event` trigger. `Custom Event` triggers will always be used to capture Formsort's analytics events (available events are shown above in [**Configure sending events in Formsort**](#configure-sending-events-in-formsort)).&#x20;

Since we'll be looking for the **FlowLoaded** event, add "FlowLoaded" as the event name.&#x20;

Don't forget to add a name to this trigger  - "Flowloaded trigger" is apt. **Save**.&#x20;

{% hint style="info" %}
FYI, if you'd like a tag to be triggered on *all* events, put `.*` as the Event name and check "Use regex matching".
{% endhint %}

![Trigger configuration screen in the GTM console](https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FX2mApROEpvMvj4tYk9il%2Fgtm-image-3.png?alt=media\&token=a5dfe0ae-dea1-408c-b6a4-5e26ce382837)

Now, open the **Tags** menu and navigate to **New** -> **Tag Configuration** -> **Custom HTML**.&#x20;

For the purposes of this section, we're going to have GTM insert a `<script>` onto our page that notifies us that GTM works when our flow loads. Add the following script to your Tag:

```
<script>alert("Hello from GTM")</script>
```

Under **Triggering**, add the **FlowLoaded** trigger we just created. **Save**.

![Creating a tag that creates a console alert](https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FWX87HJWxFTbQsELNzyVo%2Fgtm-image-4.png?alt=media\&token=519d9e61-9d86-4508-a4aa-b89dcf24c80b)

**Submit** the changes at the top right, then **Publish.**

![Click "Submit" at top right](https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FtYBsu0jZw4mijNoSG6aG%2Fgtm-image-5.png?alt=media\&token=e961860b-0780-48a0-a5d6-f8de609cd9a2)

![Once "Submit" has been clicked, hit "Publish" at top right](https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2F5gtm9OYgRgSbGJHn6op5%2Fgtm-image-6.png?alt=media\&token=2f81e91f-e4a6-4903-b4ba-142126754421)

#### Put it together&#x20;

Visit a live version of your flow - we advise doing this in an incognito window.&#x20;

When the flow loads, a console alert will be generated by GTM that shows our tag has successfully loaded when triggered by Formsort's **FlowLoaded** event.&#x20;

![Console alert script, loaded by GTM](https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FAQzNV4CVbrWuJmcL8s5E%2Fgtm-image-7.png?alt=media\&token=d79c06a5-3fd1-4d34-9ba0-256d57ed7e50)

From here, we can move on to creating tags for capturing different events.

## Google Analytics 4 (GA4)

### GA4 Google Tag

This tag is required to configure the GA4 version of Google Analytics on your flows, and should always be set up before configuring **GA4 Event** tags.&#x20;

The **Google Tag** can also be used send pageview events to GA4 via Google Tag Manager.

{% hint style="info" %}
If you still need to create a GA4 property, please see [Google's documentation](https://support.google.com/analytics/answer/9304153?hl=en\&ref_topic=9303319#zippy=%2Cweb). Follow the instruction for **Web**. &#x20;
{% endhint %}

In **Tag Configuration**, choose the **Google Analytics** -> **Google Tag** and enter the **Measurement ID** of your GA4 property.

Under **Triggering**, choose **Initialization - All Pages**. This will ensure the Google Tag fires before any other trigger. Give the tag a name and **Save.**

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2F9mrtkW50zJgmpfIROvQv%2Fimage.png?alt=media&#x26;token=53319abf-b5b5-4f47-897e-e6383c20cc82" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FfpPLvgtBxy1PrX6uIYtj%2Fimage.png?alt=media&#x26;token=9fbde4c4-f4e7-4c5d-8a11-ea8f09d9a7ea" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FYhAgqykcXRFqyZsoQRpz%2Fimage.png?alt=media&#x26;token=4099ae51-c6de-4a05-ae72-f74c873b960f" alt=""><figcaption></figcaption></figure>

### GA4 Event Tag

The **Event Tag** allows you to track the events that Formsort will send as the user completes the form. You can see the Analytics Events available for tracking [here](https://docs.formsort.com/integrations/getting-data-out/analytics), as well as in the integration menu for GTM, under **Enabled Events**.&#x20;

To continue on with the Configuring Triggers guide above, we will create a Tag that captures the `FlowLoaded` event.&#x20;

{% hint style="info" %}
See [Configuring Triggers](#configuring-triggers) if you still need to set up your GTM integration *in Formsort.*&#x20;
{% endhint %}

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2Fe6fhDxOQZiVg2fZ7viz5%2Fga4-4.png?alt=media&#x26;token=b3162e25-114d-4be3-94d6-d96fabb72f28" alt=""><figcaption><p>The GTM integration menu </p></figcaption></figure>

Click the **Tag Configuration** box and choose the **Google Analytics: GA4 Event** tag.&#x20;

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2Fpc5yDiqwEnafJdnSzGj9%2Fimage.png?alt=media&#x26;token=fdb30f16-2eef-40d0-a940-17ec9cf3ab5e" alt=""><figcaption></figcaption></figure>

Enter your **Measurement ID** in the corresponding field.

For **Event Name**, choose the name of the event you want to track. We'll use **FlowLoaded.**&#x20;

{% hint style="info" %}
Ensure case matching of our Analytics Events when adding them to **event name**.
{% endhint %}

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FkHBdzRkFTZHIQwCatxBV%2Fimage.png?alt=media&#x26;token=010adf03-5df6-4a48-a8f7-ce320a7dbfc7" alt=""><figcaption></figcaption></figure>

In **Triggering**, add the **FlowLoaded** trigger we've already created.&#x20;

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FlDGahkxVBpzWAFH16bpz%2Fimage.png?alt=media&#x26;token=91d41477-c287-4cb5-9f74-187e9f3c962d" alt=""><figcaption></figcaption></figure>

To recap, we just created a Tag called **FlowLoaded** that will send a "flow loaded" analytics event to our Google Property. This will fire when the flow is loaded, thanks to our **FlowLoaded** trigger!&#x20;

From here you can move on to the "Previewing..." section to double-check your tag setup before Publishing your GTM container. Or, if you're feeling confident, you can publish your container right away!&#x20;

## Universal Analytics

{% hint style="warning" %}
Universal Analytics has been deprecated by Google as of July 1, 2023. Please see our above section [Google Analytics 4](#ga4-google-tag).

If you need assistance transitioning your property from Universal Analytics to GA4, please see [our guide](https://docs.formsort.com/integrations/integration-reference/google-analytics/updating-from-universal-analytics-to-ga4).
{% endhint %}

## "Previewing" GTM tags in GA4&#x20;

### GTM Preview&#x20;

GA4 properties have a very helpful feature called "debug view" that will allow you to test your event tags before publishing your GTM container.&#x20;

Once you have setup a tag and trigger and want to test, click **Preview** in the top right corner of your GTM container.

Google's **Tag Assistant** tab will launch, and may require you to add the URL of the flow (or, if you're [embedding](https://docs.formsort.com/publishing-and-deployment/embedding), the URL of your website) that these tags are being configured for.

Once the URL is added, click **Connect** and then **Continue**.

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FwMx45mcASiwczt1pups4%2Fpreview1.png?alt=media&#x26;token=4568266d-97d9-464a-b86b-ec53ac823b20" alt=""><figcaption><p>The Tag Assistant launch tab </p></figcaption></figure>

The Tag Assistant tab, you can interact with the debug interface and in the **Summary Bar**, you can view which tags are firing and the order they are firing in. If you've enabled **Send Answers** in Formsort's integration menu, you would be able to view the values of **answer variables** in the `dataLayer.push()` object.&#x20;

By default, you will be able to see other pertinent information like the `event`, `flowLabel`  and `variantLabel`.&#x20;

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FWP0bD4ZfpR7CUIPB9Fvx%2Fpreview2.png?alt=media&#x26;token=f194ffb1-8cf7-4a89-bd86-098947a09ed1" alt=""><figcaption><p>Viewing tags that fired on FlowLoaded</p></figcaption></figure>

The **Summary** item can give you an aggregated look at which of your tags are firing, among other things.&#x20;

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FyWpDourrAqqzHpzbjTRD%2Fpreview3.png?alt=media&#x26;token=a6ce88ed-d2f4-4472-8d6c-0b92eb26c228" alt=""><figcaption><p>The Summary view </p></figcaption></figure>

In addition to the Tag Assistant tab, you should see your flow open in a new window. This will allow you to interact with your flow and test the different triggers and tags you have set up.&#x20;

### GA4 DebugView

In your **GA4 properties**, you can also check the reception of these events using **DebugView**.&#x20;

To get to **DebugView**, open your GA4 property in Google Analytics and head to **Admin**.&#x20;

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FlcrxeV1yrzDBPNMHmErh%2Fpreview4.png?alt=media&#x26;token=5b37dee6-fbb4-45ac-867c-7a5274fbb2c4" alt=""><figcaption><p> </p></figcaption></figure>

In the **Property** column, scroll down to **DebugView**.

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FiBjtWKcFT45QrBlqCm6J%2Fpreview6.png?alt=media&#x26;token=11034419-5822-4cdd-8e98-c69a3b33d5e7" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2F90hb7s70CeilUAAkPKpq%2Fpreview5.png?alt=media&#x26;token=3edf04e4-a5a4-42de-8d00-96c370e3cbcd" alt=""><figcaption></figcaption></figure>

With this screen open, you can view the various tag events you've created, as well as the **event parameters** and **user properties** associated with them.&#x20;

When you're done testing and everything looks good, head back to your GTM container and publish it!&#x20;
