google-analyticsgoogle-tag-managergoogle-analytics-apigoogle-analytics-firebase

Send Event to GTM server container from a back-end


We are using a GTM container server.

We want to send different events from the back-end of our web application directly to our GTM server container.

The idea is to be able to activate tags that will send these events to different sources (Mixpanel, SendinBlue,...)

In the GTM server-side documentation we discovered that it is possible to use the measurement protocol used by Google Analytics.

To do this, you need to create or use a MeasurementProtocol client GTM server-side client

So we created a new client with the path /mp/collect and tried to use the MeasurementProtocole for GA4 because we also use GA4 for Analytics. https://developers.google.com/analytics/devguides/collection/protocol/ga4

But in the example request, it is asked for a measurementId and an apiSecret. These are provided by GA. What information should we give if we want to send the event to the GTM server client directly?

This is our script to test:

function myFunction() {
const measurementId = `G-XXXXXXX`;
const apiSecret = `FFFFFFFFFFFFDDDDDDDDD`;

UrlFetchApp.fetch(`https://xxxxx.xxxxxxxx.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, {
  method: "POST",
  payload: JSON.stringify({
    "client_id": "client_id",
    "events": [{
      "name": "generate_lead",
      "params": {
        "currency": "USD",
        "value": 99.99
      }
    }]
  })
});
}

But we have this error enter image description here


Solution

  • So to be able to send events from my back-end to sGTM (GTM Server container) I used this repo on Github:

    https://github.com/square/server-to-server-gtm-client

    1 - To install it you must download the template.tpl file from the repo.

    2 - Then on sGTM you go at the bottom of the left menu and click on "Template"

    3 - You must click on New in the Client Template section.

    4 - After you can click on the three dot menu in the top right corner and select import.

    5 - This is the moment where you import the template.tpl file you download before.

    6 - Now you can click on "Client" item on the left menu and create a new Client

    7 - For the client configuration select the template you created before.

    8 - Then follow the readme of the repo on Github :-)

    With this solution you will be able to send event from your back-end directly to the sGTM container (server).