Google Tag Manager – Server-Side Tracking on Ubuntu 22
In this comprehensive tutorial, we will guide you through the process of setting up GTM (Google Tag Manager) server-side tracking using Nginx, Certbot, and Docker on an Ubuntu 22.04.2 server. By following the steps outlined below, you will be able to configure a robust and secure environment for managing your tags and tracking events.
Prerequisites:
- Ubuntu 22.04.2 installed on your server
- Access to a terminal or SSH client
- 2 subdomains pointing to your servers ip address (we will use gtm.example.com and gtmp.example.com)
- sudo privileges
Table of Contents
Update Your Google Tag Manager System
Before installing any packages, it’s important to make sure your system is up-to-date. To do this, open a terminal and run the following command:
sudo apt-get update && sudo apt-get -u upgrade
This command will update the package lists and upgrade any outdated packages on your system.
Install Nginx, Certbot, and Docker for your Google Tag Manager SST
To install Nginx, Certbot, and Docker, run the following command:
sudo apt-get install certbot docker.io nginx
This command will install the required packages on your system.
Stop the Nginx Service
Before configuring Nginx, stop the Nginx service by running the following command:
sudo service nginx stop
This will stop the Nginx service and allow you to modify the Nginx configuration files.
Obtain SSL/TLS Certificates with Certbot
Next, you will use Certbot to obtain SSL/TLS certificates for your domains. In this example, we will obtain certificates for gtm.example.com and gtmp.example.com.
sudo certbot certonly --standalone -d gtm.example.com,gtmp.example.com --agree-tos --register-unsafely-without-email
This command will start the Certbot standalone server and obtain SSL/TLS certificates for the specified domains. The –agree-tos option will agree to the Let’s Encrypt terms of service, and the –register-unsafely-without-email option will skip the email registration step.
Create Nginx Configuration Files
Next, you will create Nginx configuration files for your domains. Open the Nginx sites-available directory by running the following command:
cd /etc/nginx/sites-available
Remove the default configuration file by running the following command:
sudo rm -rf default
Create a new file for redirecting HTTP traffic to HTTPS by running the following command:
sudo nano redirect_http2https
Paste the following code into the file:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
Save and exit the file by pressing CTRL+X, then Y, then ENTER.
Create a new file for gtm.example.com by running the following command:
sudo nano gtm.example.com
Paste the following code into the file:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name gtm.example.com;
gzip on;
gzip_disable "msie6";
gzip_min_length 100;
#### Lets Encrypt ACME Challenge
location ^~ /.well-known/acme-challenge/ {
allow all;
root /var/lib/letsencrypt/;
default_type "text/plain";
try_files $uri =404;
}
#### Lets Encrypt ACME Challenge
#### Proxy to docker backend with GTM ####
location / {
proxy_pass http://localhost:8080;
}
ssl_certificate /etc/letsencrypt/live/gtm.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/gtm.example.com/privkey.pem;
}
Save and exit the file by pressing CTRL+X, then Y, then ENTER.
Create a new file for gtmp.example.com by running the following command:
sudo nano gtmp.example.com
Paste the following code into the file:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name gtmp.example.com;
gzip on;
gzip_disable "msie6";
gzip_min_length 100;
location ^~ /.well-known/acme-challenge/ {
allow all;
root /var/lib/letsencrypt/;
default_type "text/plain";
try_files $uri =404;
}1
#### Proxy to docker backend with GTM ####
location / {
proxy_pass http://localhost:8079;
}
ssl_certificate /etc/letsencrypt/live/gtm.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/gtm.example.com/privkey.pem;
}
Save and exit the file by pressing CTRL+X, then Y, then ENTER.
Enable Nginx Configuration Files (continued)
Once you have created the configuration files, you need to enable them by creating symbolic links to the sites-available directory. Run the following commands to create the symbolic links:
cd /etc/nginx/sites-enabled/
sudo ln -s ../sites-available/gtm.example.com ./
sudo ln -s ../sites-available/gtmp.example.com ./
sudo ln -s ../sites-available/redirect_http2https ./
This will create symbolic links in the sites-enabled directory that point to the configuration files you created.
Start Nginx
Now that you have created the configuration files, you can start Nginx and Docker. Run the following commands to start Nginx and Docker:
sudo service nginx start
Setup and start Docker Container
In this step, you will need the CONTAINER_CONFIG variable, which can be obtained from your GTM Server-Side container. If you’re not familiar with how to obtain the CONTAINER_CONFIG, we recommend referring to our tutorial on retrieving the CONTAINER_CONFIG variable. This tutorial will provide you with step-by-step instructions on how to obtain the necessary configuration. You can find the tutorial here.
Explanation: The mentioned step requires the CONTAINER_CONFIG variable, which is specific to your Google Tag Manager Server-Side container. This variable contains essential configuration details needed for the setup. As the process of obtaining the CONTAINER_CONFIG may vary, we have created a separate tutorial dedicated to that topic. By following the tutorial, you will gain a clear understanding of how to retrieve the CONTAINER_CONFIG variable for your Google Tag Manager Server-Side container.
sudo docker run -d --name gtm-preview -p 8079:8080 -e CONTAINER_CONFIG='REPLACE_THIS_WITH_YOUR_CONTAINER_CONFIG_TOKEN' -e RUN_AS_PREVIEW_SERVER=true gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable
sudo docker run -d --name gtm-live -p 8080:8080 -e CONTAINER_CONFIG='REPLACE_THIS_WITH_YOUR_CONTAINER_CONFIG_TOKEN' -e PREVIEW_SERVER_URL='https://gtmp.example.com' gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable
The first and second commands start Docker containers for Google Tag Manager preview and live, respectively.
Conclusion
Congratulations on successfully setting up Google Tag Manager server-side tracking using Nginx, Certbot, and Docker on your Ubuntu server! By following the steps in this tutorial, you have created a robust and secure environment for managing your tags and tracking events.
With server-side tracking in place, you can now enjoy enhanced analytics capabilities and gain valuable insights from your website or application. This setup allows you to efficiently handle and process data on the server side, providing improved performance and flexibility.
If you’re interested in setting up client-side tracking alongside server-side tracking, we also have a tutorial available on that topic. It will guide you through the process of configuring Google Tag Manager for client-side tracking, complementing the server-side setup.
Thank you for following this tutorial, and we hope you find great value in leveraging server-side tracking for your analytics needs. Keep exploring and utilizing the power of Google Tag Manager to unlock deeper insights into your online presence!
For more articles like this, please check out our blog section here.
Leave a Reply