1

I had GTM set up on my website and I can send dataLayer to my GTM containers just fine. I wonder if I can do the same from my backend? My backend runs on spring boot and I am having difficulty finding any documentation. My use case is that I want to send dataLayer to my GTM containers when payments are received from customers. From client side it will be difficult because payment successful page can be loaded multiple times; giving incorrect result.

How do I send dataLayer to my GTM container from a backend microservice?

user1955934
  • 3,185
  • 5
  • 42
  • 68

2 Answers2

0

You can do that, but it might be more work than it's worth.

First, you can obviously connect to your backend via an Ajax request and push data to the datalayer in your success callback. If you include an "event" key, you can fire tags in GTM accordingly. This is probably not what you mean, and it won't solve your problem.

For some time now, Google offers also a server-side Google Tag Manager. This is set up as a docker container in "the Cloud" (Google presumes GCP, but it will also run on AWS an everywhere else where you can run docker containers).

This server-side GTM receives http requests from your website (or other sources, such as your backend) and distributes them to one or more configured tags. This reduces the amount of code in the frontend, allows to rewrite javascript cookies to cookie headers (thereby evading tracking protection) an d allows for more privacy by redacting data before it is passend on to the marketing vendors.

Server-side GTM does not fully replace GTM, instead it is complementary: You probably want to keep your client-side GTM to set up triggers by which to send requests to the server-side part.

You can also use this to integrate backend services; the idea is that your service send a request to server-side GTM, where it is then cached and available to to be used in tags.

The (very much recommended) blog of Lukas Oldenburg has an example where this is used to stitch together user information, but the example could be adapted to other use cases: https://lukas-oldenburg.medium.com/use-google-tag-manager-server-side-for-visitor-stitching-by-querying-a-gcp-database-in-real-time-644001068a1c.

But that means you need to set up a cloud environment etc., not an unsurmountable amount of work, but not something you would do without a convincing business case, either.

Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
  • hmm it doesn't sound very straight-forward. I was thinking it'd be great if I can send a rest API request to google's endpoint, identifying myself as the right container admin and specifying data layer targeted to that container... do we not have that kind of API? – user1955934 Sep 10 '21 at 02:05
  • The "standard" GTM is a javascript injector without a server-side component (it bundles all tags into a javascript file together with some boilerplate code and injects it into the page), so more or less by definition it does not have a server-side API for tracking (you can create the container via an API, but that's not what you are looking for). Depending on which tracking services you use it might be easier to bypass GTM and send requests directly to their endpoints (measurement protocol for GA, conversion API for Facebook etc). – Eike Pierstorff Sep 10 '21 at 06:46
0

@eike-pierstorff The answer is good. However, with the use of Server GTM, you can do everything much easier.

As you mentioned before you can send data from your server to GTM Server with all needed info and then sGTM will send this info to other vendors like FB, GA, Klaviyo, etc.

For example, you can check this article on how to send data from your CRM to Facebook conversion API using webhooks

Bukashk0zzz
  • 343
  • 3
  • 13