Skip to content

Server-side Google Tag Manager

Send events from your server-side Tag Manager container to My Journey Analytics with our tag template, and send My Journey Analytics events on to your server container.

A server-side Tag Manager container receives your site's measurement requests on a domain you control and passes each event on to the tags you choose. This guide covers both directions:

  • Into My Journey Analytics: our tag template sends each event your server container receives to your project.
  • Out of My Journey Analytics: the Google Analytics 4 destination in the app sends your project's events to your server container or straight to Google Analytics 4.

If you only use a web container, see Install with Google Tag Manager instead.

Before you start

You need:

  • a server container that is already receiving events, usually from a Google tag on your site;
  • your project key, shown in Project settings in the app;
  • the address to send events to: your custom tracking domain, such as https://t.example.com, or your region's address, which is https://app.myjourneyanalytics.com for the Australian region. It must start with https://.

Send events to My Journey Analytics

1. Import the tag template

  1. Download the template: my-journey-analytics-sgtm-tag.tpl.
  2. In your server container, open Templates and click New under Tag Templates.
  3. In the template editor, open the More Actions menu (the three dots) and choose Import.
  4. Select the file you downloaded, then click Save and close the editor.

The template appears under Tag Templates as My Journey Analytics. Its Permissions tab lists what it may do: read event data, read cookies, send HTTPS requests and write to the preview console.

2. Create the tag

  1. Open Tags, click New and choose My Journey Analytics as the tag type.
  2. Fill in the fields described below.
  3. Choose a trigger for the events you want to send. To avoid sending events twice, fire it only for requests from your site's Google tag, not for the events this guide's second half sends back to your container.
  4. Save, test in Preview mode, then Submit to publish.

The tag's fields

FieldWhat to enter
Tracking domainYour tracking domain or region's address, starting with https://.
Project keyYour project key from Project settings.
Read the visitor ID fromWhere each event's visitor ID comes from. See Use the same visitor as the browser.
Person profilesMatch the person_profiles option of the web library on your site. Identified users only means anonymous visitors' events do not create person profiles.
Identify usersWhen ticked, events that carry a user_id are recorded against that ID. On the events you list, such as login and sign_up, the tag also links the visitor's earlier anonymous ID to the user.
Person properties to setProperties stored on the person, such as a plan name, when the event creates or updates a person profile.
Record page_view as a pageviewRenames page_view to the pageview event the product's reports use. On by default.
Rename eventsSends an incoming event under another name, for example generate_lead as lead_created.
Rename standard web fieldsStores the page address, referrer, title, host, path, browser language and user agent under the names the product's reports use. On by default.
Send the visitor's IP addressUses the visitor's address from the event, rather than your server's, to work out their location. Projects set to discard IP addresses still discard it.
Event ID parameterOptional: an event parameter holding a UUID for the event. Values that are not UUIDs are not sent.
Event parameters not to sendParameters to leave out. End a name with * to leave out every parameter that starts with it.

Every other event parameter is sent as an event property. The tag never sends the container's internal parameters (those starting with x-), user_data, which can hold email addresses and phone numbers, or the fields it uses itself (event_name, client_id, user_id and ip_override).

Events are recorded at the time they reach us, which for a server container is a moment after they happen.

What the tag sends

For each event, the tag makes one HTTPS request:

text
POST https://t.example.com/i/v0/e/
Content-Type: application/json

{
  "api_key": "your project key",
  "batch": [
    {
      "event": "$pageview",
      "distinct_id": "the visitor's ID",
      "properties": {
        "$current_url": "https://www.example.com/pricing",
        "title": "Pricing",
        "$lib": "my-journey-sgtm"
      }
    }
  ]
}

A response with status 200 means the event was accepted, and the tag reports success. Any other status, or no response within five seconds, is reported as a failure in Tag Manager.

Use the same visitor as the browser

If your site also runs the My Journey Analytics web library, you will usually want server events to belong to the same visitor and session as the browser's.

  • The My Journey Analytics browser library's cookie (recommended): the tag reads the library's own cookie from the incoming request and uses its visitor ID, device ID and session ID. This works when your server container runs on a subdomain of your site, such as sgtm.example.com, and the library's cross_subdomain_cookie option is on, so the cookie is sent with requests to that subdomain. In Preview mode, the Request tab shows whether the cookie arrived.
  • A cookie you name: use a first-party visitor cookie that your site or server sets. To make the browser use the same ID, start the web library with it as shown in Use an existing visitor ID.
  • An event parameter you name: for example a visitor_id parameter your Google tag sends.
  • The event's client_id (the default): the visitor ID from your Google tag. It is not the web library's ID, so the same visitor appears as two people unless they are identified on both sides.

If the chosen source is empty for an event, the tag falls back to the event's client_id. An event with no visitor ID at all is not sent.

Avoid counting events twice

My Journey Analytics does not remove duplicate events when they arrive. Two events sent with the same event ID are both stored, and both appear in reports until our storage merges them in the background. That merge happens at an unpredictable time, and only for events that also share the same name, visitor ID and day. So treat the event ID as a safety net for retries, not as a way to send everything twice.

Instead, send each event by one route only:

  • If the web library is on your site, it already records pageviews, clicks and form submissions. Add a condition to the tag's trigger so it skips page_view, and use the tag for events only your server sees, such as purchases confirmed by your payment system.
  • If the web library is not on your site, the tag can send everything, pageviews included.

Troubleshooting

  • Nothing arrives, but the tag reports success: check the project key. A key that belongs to no project is accepted and then discarded.
  • Status 401: the project key is missing or not a project key.
  • Status 400: the request could not be read. Preview mode's Console shows the request the tag built.
  • Status 429 or 503: the event was not stored because too many arrived at once or the service was briefly unavailable. The tag does not retry, so a burst of these means events were lost; contact support if it persists.
  • The tag fails without sending: check the tracking domain starts with https://, and that the event has a visitor ID.

Send My Journey Analytics events to your server container

The Google Analytics 4 destination sends events from your project as Measurement Protocol requests, either straight to Google Analytics 4 or to your server container.

1. Prepare Google Analytics 4 and your container

  1. In Google Analytics, open Admin → Data collection and modification → Data streams, select your web stream, and copy its Measurement ID.
  2. On the same stream, open Measurement Protocol API secrets, click Create, give the secret a name and copy its value.
  3. To send to your server container, open Clients in the container, click New and add a Measurement Protocol client. Note its activation path; the destination's Collection URL must end with it. Publish the container.

2. Create the destination

  1. In the app, open Destinations and click New destination.
  2. Choose Google Analytics 4.
  3. Enter the Measurement ID and API secret.
  4. Set the Collection URL: - to send straight to Google Analytics 4, keep https://www.google-analytics.com/mp/collect (or use https://region1.google-analytics.com/mp/collect to collect in the EU); - to send to your server container, use your container's address followed by the client's activation path, such as https://sgtm.example.com/mp/collect if that is the path you set.
  5. Check the Client ID, User ID, Event name, Event parameters and User properties fields, and save.

By default the destination sends pageviews only, as page_view, with page_location, page_referrer and page_title. Add other events to its filters and give each a Google Analytics name: up to 40 letters, numbers and underscores, starting with a letter. Names like $autocapture or Order Completed are rejected by Google Analytics.

Measurement Protocol requests for a web stream need a client_id, which ties each event to a browser. The destination uses the visitor's My Journey Analytics ID by default. To join events up with the sessions your Google tag records, capture the Google client ID as an event property and map Client ID to it. Map User ID to the ID you identify users with.

Can't find what you need? Contact support