How to set up Data Layer for GTM Server Side Container
In this article, I am going to talk about how to set up a data layer for a GTM server-side container.
Purpose of a data layer
The data layer corresponds to the data structure that contains different data variables.
The data layer should contain all the data you want to send to various analytics and/or marketing tags.
In the context of Google Tag Manager, a data layer is a javascript array of variables that stores different values such as page title, page URL, page category, user ID, client ID, etc. This data layer is loaded when the website page gets loaded on the browser.
Once the page load is complete, the client-side GTM container captures the values and stores it in the form of variables. When any tag is set to fire you can also pass the values from the data layer to Google Analytics or other marketing tools.
To know more about the data layer you can read more here: Google Tag Manager Data Layer Tutorial with Examples
Does the server-side GTM container require a data layer?
When it comes to the server-side GTM container, it’s not loaded on the browser and resides in the Google Cloud environment.
Now you may be thinking about how to set up a data layer for a GTM container that is not even available on the browser. Another question that arises is whether the server-side GTM container requires a data layer? Well, the answer to this question is not simple but I will try to explain it using some scenarios.
Scenario 1: When you set a Google Analytics tag (either GA4 or Universal Analytics) in the client-side GTM container, you set it in such a way that all your data layer variables are mapped to a particular parameter, like custom dimension, custom metric, ecommerce data variables, etc.
When the website loads, the client-side container gets its value from the data layer and passes it to the server-side container using HTTP requests. The server-side container then interprets the requests and forwards them to Google Analytics.
In this scenario, if you set custom dimension #1, let’s suppose ‘User State’ which can have values like ‘logged in’ or not ‘logged in’. This variable will get its value from the data layer while loading the page and will be sent in the HTTP request to the server-side GTM container.
Now the server-side container will interpret the incoming HTTP request and pass this information in the same format to Google Analytics without any changes. You can see that in the below image, I am firing a pageview tag and passing custom dimension #1 as ‘User State’.
The same information is continued in the server-side GTM container and when the server-side tag is fired it also has dimension #1 set to ‘logged in’ which is nothing but that value of our data layer variable ‘User State’.
Now this answers our question; If we set up client-side tags correctly to a value from the data layer and pass it in a particular parameter like custom dimension, custom metric, ecommerce data variables, etc. it will be retained as it is in the server-side container as well, and you do not need to have a separate data layer for server-side GTM container.
Scenario 2: Now let’s consider a situation where you want to send data to a third-party tool, like Facebook, and you want to have a value from your data layer variable which is available in the browser (also in the client-side GTM container).
You would like to have the same variable value that is also available in the server-side container. You cannot use or interpret the custom dimensions, metrics, or ecommerce variables from the Universal Analytics tag.
Here the GA4 configuration tag comes to the rescue and you can use that as a medium to send values from the client-side GTM container to the server-side GTM container.
Using GA4 tag parameters you can send values from client-side GTM container to server-side GTM container. Then you can use those values as event data variables in the server-side container.
Now let’s see how you can achieve this.
Get weekly practical tips on GA4 and/or BigQuery to accurately track and read your analytics data.
How to pass a data layer variable to a server-side GTM container?
To pass a data layer variable from a client-side GTM container to a server-side GTM container you need to create dummy GA4 events.
To know more follow the below steps.
Note: I am assuming that you have already set up your data layer variables in GTM
Step-1: Navigate to your client-side GTM container and click on ‘Tags’ and then click on ‘New’.
Step-2: Give your tag a descriptive name and click on ‘Tag Configuration’.
Step-3: Select ‘Google Analytics: GA4 Configuration’.
Step-4: Now add your GA4 measurement ID and click on ‘Fields to Set’.
Step-5: Click on ‘Add Row’ and then the first field name to add is ‘transport_url’ and its value should be your server-side container URL (you can find it in the admin section of the server-side container).
If you don’t know how to get it you can read more here: How to set up GA4 via GTM Server Side Tagging
Step-6: Click on ‘Add row’ and in the field name type the variable name that you want to export to the server-side container, for example, ‘page_category’ or ‘user_state’. In the ‘Value’ section select the data layer variable from which it will get the value.
Step-7: Now apply the default trigger ‘All Pages’ to the tag and your configuration will look like below.
Step-8: Now save the tag and preview the client-side container. You should see that the GA4 configuration tag is fired with data layer values in it, like below.
Step-9: At the same time, put your server-side GTM container in preview mode and you should see the incoming GA4 client request like below.
Step-10: For the corresponding request, click on ‘Events Data’ in the console.
Step-11: You should be able to see the variable name that we set in the client-side container along with its value populated.
Congratulations! You have successfully passed the data layer variable from a client-side GTM container to a server-side GTM container.
But hold on, these data layer variable values are still available in events data requests only and now we have to create a new event data variable in the server-side container so that it can hold these values.
How to use client-side data layer variables in a server-side GTM container
Follow the below steps to create a new ‘Events data’ variable in the server-side GTM container which will inherit its values from the events parameters of the GA4 configuration tag, and ultimately from the data layer.
Step-1: Navigate to your server-side GTM container and click on ‘Variable’ and then click on ‘New’ under ‘User-defined variables’.
Step-2: Give your variable a descriptive name and click on ‘Variable configuration’.
Step-3: Now select ‘Event data’ as the variable type.
Step-4: Now type the exact key path for the particular variable that we set in the client-side GTM container.
In other words, the ‘Field Name’ of the client-side GTM container should be the ‘Key Path’ of the server-side container for the corresponding variable.
For example, We set the field name as ‘page_category’ in the client-side container, and it’s the key path of the server-side variable.
You can also get the key path in the ‘Events data’ tab which is in the server-side container preview mode.
Step-5: Now save the variable and repeat the same process from steps 1-4 for the other variables as well.
Step-6: Now put your server-side GTM container in preview mode again and visit the webpage.
For corresponding GA4 client requests click on ‘Variables’ in the console and you should be able to see the variables that we created in the server-side container has the vales from the original data layer variable like below.
Congratulations! You have successfully set up a data layer for a GTM server-side container. Now you can use these variables in any third-party tag.
Other Articles on Server Side Tracking
- Server Side Tracking for Google Analytics via GTM
- How to Create Server Side Container for Google Tag Manager
- How to Configure DNS for GTM Server Side Container
- How to send data from Website to Server Side Container
- How to set up GA4 via GTM Server Side Tagging
- Setting up Facebook Conversion API via Google Tag Manager (GTM)
- Google Tag Manager Tutorial
- GTM Server Side Preview Mode – Tutorial
Frequently asked questions about how to set up a data layer for GTM server-side container.
How can I use the data layer in the server-side GTM container?
You cannot use the data layer directly on the server-side GTM container.
The GA4 configuration tag can be used as a medium to send values from the client-side GTM container (data layer) to the server-side GTM container.
Using GA4 tag parameters you can send values from a client-side GTM container to a server-side GTM container. Then you can use those values as event data variables in the server-side container.
Can I import my client-side GTM container to a server-side GTM container and use it?
Client-side GTM containers and server-side GTM containers are completely different things, so you cannot import your client-side GTM container and use it in the cloud environment. The server container serves as a server-side proxy/API gateway/endpoint.
Does the server-side GTM container require a data layer?
When it comes to server-side GTM containers, it is not loaded on the browser and resides in the Google Cloud environment. When it comes to setting up Google Analytics (either Universal Analytics or GA4) on the server-side, you don’t need data layer variables because the incoming HTTP requests have it all from the client-side GTM container.
However, if you want to set up third-party tags like Facebook you need different variables to store value. This can be achieved by using the GA4 tag where you can pass data layer variables as field parameters from the client-side to the server-side container.
In this article, I am going to talk about how to set up a data layer for a GTM server-side container.
Purpose of a data layer
The data layer corresponds to the data structure that contains different data variables.
The data layer should contain all the data you want to send to various analytics and/or marketing tags.
In the context of Google Tag Manager, a data layer is a javascript array of variables that stores different values such as page title, page URL, page category, user ID, client ID, etc. This data layer is loaded when the website page gets loaded on the browser.
Once the page load is complete, the client-side GTM container captures the values and stores it in the form of variables. When any tag is set to fire you can also pass the values from the data layer to Google Analytics or other marketing tools.
To know more about the data layer you can read more here: Google Tag Manager Data Layer Tutorial with Examples
Does the server-side GTM container require a data layer?
When it comes to the server-side GTM container, it’s not loaded on the browser and resides in the Google Cloud environment.
Now you may be thinking about how to set up a data layer for a GTM container that is not even available on the browser. Another question that arises is whether the server-side GTM container requires a data layer? Well, the answer to this question is not simple but I will try to explain it using some scenarios.
Scenario 1: When you set a Google Analytics tag (either GA4 or Universal Analytics) in the client-side GTM container, you set it in such a way that all your data layer variables are mapped to a particular parameter, like custom dimension, custom metric, ecommerce data variables, etc.
When the website loads, the client-side container gets its value from the data layer and passes it to the server-side container using HTTP requests. The server-side container then interprets the requests and forwards them to Google Analytics.
In this scenario, if you set custom dimension #1, let’s suppose ‘User State’ which can have values like ‘logged in’ or not ‘logged in’. This variable will get its value from the data layer while loading the page and will be sent in the HTTP request to the server-side GTM container.
Now the server-side container will interpret the incoming HTTP request and pass this information in the same format to Google Analytics without any changes. You can see that in the below image, I am firing a pageview tag and passing custom dimension #1 as ‘User State’.
The same information is continued in the server-side GTM container and when the server-side tag is fired it also has dimension #1 set to ‘logged in’ which is nothing but that value of our data layer variable ‘User State’.
Now this answers our question; If we set up client-side tags correctly to a value from the data layer and pass it in a particular parameter like custom dimension, custom metric, ecommerce data variables, etc. it will be retained as it is in the server-side container as well, and you do not need to have a separate data layer for server-side GTM container.
Scenario 2: Now let’s consider a situation where you want to send data to a third-party tool, like Facebook, and you want to have a value from your data layer variable which is available in the browser (also in the client-side GTM container).
You would like to have the same variable value that is also available in the server-side container. You cannot use or interpret the custom dimensions, metrics, or ecommerce variables from the Universal Analytics tag.
Here the GA4 configuration tag comes to the rescue and you can use that as a medium to send values from the client-side GTM container to the server-side GTM container.
Using GA4 tag parameters you can send values from client-side GTM container to server-side GTM container. Then you can use those values as event data variables in the server-side container.
Now let’s see how you can achieve this.
How to pass a data layer variable to a server-side GTM container?
To pass a data layer variable from a client-side GTM container to a server-side GTM container you need to create dummy GA4 events.
To know more follow the below steps.
Note: I am assuming that you have already set up your data layer variables in GTM
Step-1: Navigate to your client-side GTM container and click on ‘Tags’ and then click on ‘New’.
Step-2: Give your tag a descriptive name and click on ‘Tag Configuration’.
Step-3: Select ‘Google Analytics: GA4 Configuration’.
Step-4: Now add your GA4 measurement ID and click on ‘Fields to Set’.
Step-5: Click on ‘Add Row’ and then the first field name to add is ‘transport_url’ and its value should be your server-side container URL (you can find it in the admin section of the server-side container).
If you don’t know how to get it you can read more here: How to set up GA4 via GTM Server Side Tagging
Step-6: Click on ‘Add row’ and in the field name type the variable name that you want to export to the server-side container, for example, ‘page_category’ or ‘user_state’. In the ‘Value’ section select the data layer variable from which it will get the value.
Step-7: Now apply the default trigger ‘All Pages’ to the tag and your configuration will look like below.
Step-8: Now save the tag and preview the client-side container. You should see that the GA4 configuration tag is fired with data layer values in it, like below.
Step-9: At the same time, put your server-side GTM container in preview mode and you should see the incoming GA4 client request like below.
Step-10: For the corresponding request, click on ‘Events Data’ in the console.
Step-11: You should be able to see the variable name that we set in the client-side container along with its value populated.
Congratulations! You have successfully passed the data layer variable from a client-side GTM container to a server-side GTM container.
But hold on, these data layer variable values are still available in events data requests only and now we have to create a new event data variable in the server-side container so that it can hold these values.
How to use client-side data layer variables in a server-side GTM container
Follow the below steps to create a new ‘Events data’ variable in the server-side GTM container which will inherit its values from the events parameters of the GA4 configuration tag, and ultimately from the data layer.
Step-1: Navigate to your server-side GTM container and click on ‘Variable’ and then click on ‘New’ under ‘User-defined variables’.
Step-2: Give your variable a descriptive name and click on ‘Variable configuration’.
Step-3: Now select ‘Event data’ as the variable type.
Step-4: Now type the exact key path for the particular variable that we set in the client-side GTM container.
In other words, the ‘Field Name’ of the client-side GTM container should be the ‘Key Path’ of the server-side container for the corresponding variable.
For example, We set the field name as ‘page_category’ in the client-side container, and it’s the key path of the server-side variable.
You can also get the key path in the ‘Events data’ tab which is in the server-side container preview mode.
Step-5: Now save the variable and repeat the same process from steps 1-4 for the other variables as well.
Step-6: Now put your server-side GTM container in preview mode again and visit the webpage.
For corresponding GA4 client requests click on ‘Variables’ in the console and you should be able to see the variables that we created in the server-side container has the vales from the original data layer variable like below.
Congratulations! You have successfully set up a data layer for a GTM server-side container. Now you can use these variables in any third-party tag.
Other Articles on Server Side Tracking
- Server Side Tracking for Google Analytics via GTM
- How to Create Server Side Container for Google Tag Manager
- How to Configure DNS for GTM Server Side Container
- How to send data from Website to Server Side Container
- How to set up GA4 via GTM Server Side Tagging
- Setting up Facebook Conversion API via Google Tag Manager (GTM)
- Google Tag Manager Tutorial
- GTM Server Side Preview Mode – Tutorial
Frequently asked questions about how to set up a data layer for GTM server-side container.
How can I use the data layer in the server-side GTM container?
You cannot use the data layer directly on the server-side GTM container.
The GA4 configuration tag can be used as a medium to send values from the client-side GTM container (data layer) to the server-side GTM container.
Using GA4 tag parameters you can send values from a client-side GTM container to a server-side GTM container. Then you can use those values as event data variables in the server-side container.
Can I import my client-side GTM container to a server-side GTM container and use it?
Client-side GTM containers and server-side GTM containers are completely different things, so you cannot import your client-side GTM container and use it in the cloud environment. The server container serves as a server-side proxy/API gateway/endpoint.
Does the server-side GTM container require a data layer?
When it comes to server-side GTM containers, it is not loaded on the browser and resides in the Google Cloud environment. When it comes to setting up Google Analytics (either Universal Analytics or GA4) on the server-side, you don’t need data layer variables because the incoming HTTP requests have it all from the client-side GTM container.
However, if you want to set up third-party tags like Facebook you need different variables to store value. This can be achieved by using the GA4 tag where you can pass data layer variables as field parameters from the client-side to the server-side container.
My best selling books on Digital Analytics and Conversion Optimization
Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.
Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.
Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.
Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.