• +40 (750) 43 77 44 [email protected]
Mon - Fri | 09:00 - 16:00
WhatsApp
Blog
DevSnit
  • Home
  • About us
  • Services
    • Digital Marketing
      • Search Engine Optimisation (SEO)
      • Search Engine Marketing (SEM)
      • SEO Content Writing Services
      • Local SEO
      • Technical SEO
      • Link Building Services
      • PPC Advertising
      • Digital Marketing Strategy
    • Web Services
      • Web development
      • Custom Web Design
      • WordPress Web Design
      • eCommerce Web Design
      • GDPR Solutions
      • eMail Marketing Services
      • SMS Marketing Services
      • Website Hosting Services
    • Social Media Marketing
      • Social Media Brand Management
      • Social Media Advertising
      • Social Media Reputation Management
      • Social Media Job Ads
    • Innovation
      • Customer relationship management (CRM)
      • Daily Process Automation
      • Email Automation
      • Leads Workflow Automation
      • AI Content Writing and Publishing
      • SMS Automation
  • Case studies
  • Contact
  • English
    • Română
    • English
Instant quote
Search
Quote
  • Home
  • About us
  • Services
    • Digital Marketing
      • Search Engine Optimisation (SEO)
      • Search Engine Marketing (SEM)
      • SEO Content Writing Services
      • Local SEO
      • Technical SEO
      • Link Building Services
      • PPC Advertising
      • Digital Marketing Strategy
    • Web Services
      • Web development
      • Custom Web Design
      • WordPress Web Design
      • eCommerce Web Design
      • GDPR Solutions
      • eMail Marketing Services
      • SMS Marketing Services
      • Website Hosting Services
    • Social Media Marketing
      • Social Media Brand Management
      • Social Media Advertising
      • Social Media Reputation Management
      • Social Media Job Ads
    • Innovation
      • Customer relationship management (CRM)
      • Daily Process Automation
      • Email Automation
      • Leads Workflow Automation
      • AI Content Writing and Publishing
      • SMS Automation
  • Case studies
  • Contact
  • English
    • Română
    • English
Server Sided Tracking using Google Tag Manager

Google Tag Manager – Server-Side Tracking on Ubuntu 22

shiznit2023-12-13T17:05:03+00:00
shiznit Tutorials docker, google tag manager, server side tracking, ubuntu 22 0 Comments

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
  • Install Nginx, Certbot, and Docker for your Google Tag Manager SST
  • Stop the Nginx Service
  • Obtain SSL/TLS Certificates with Certbot
  • Create Nginx Configuration Files
  • Enable Nginx Configuration Files (continued)
  • Start Nginx
  • Setup and start Docker Container
  • Conclusion

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.

4.9/5 - (26 votes)

Share this post

Facebook Twitter LinkedIn Google + Email

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Curiosity Corner

Whether you're seeking to understand the 'why' behind the 'what' or just looking to indulge your thirst for learning, this space is crafted for you.

Recent posts

  • How to Install an SSL Proxy Server on Linux with User Authentication November 13, 2024
  • SEO Optimization: Increase your online visibility and get results in 2024 February 29, 2024
  • What is an email marketing campaign and how can you run one February 27, 2024
  • How to run Google Ads campaigns: step-by-step guide February 1, 2024
  • N8N Workflows & Credentials Migration: Export & Import Tutorial January 15, 2024

automation container_config crm software docker email marketing email promo google ads google analytics 4 google seo google tag manager google tracking gtm gtm account improve form conversion manage business n8n n8n migration newsletter onpage seo pay per click plugin creation ppc ppc campaign proxy server ubuntu search engine optimization sem seo optimization seo tricks server side server side tracking setup campaigns ssl proxy server technical seo ubuntu ubuntu 22 website seo wordpress svn wp plugin

Business development solutions

Contact us

SEO, AdWords, Automatizari, Dezvoltare Web - Solutii simple pentru conexiuni complexe

Devsnit Ignited S.R.L., your ultimate destination for unparalleled digital marketing solutions. Attract, impress and convert more leads online and get results with Devsnit.

WhatsApp
+40 (750) 43 77 44
[email protected]
Str. Blaj, Nr. 6, 800472, Galati

Working hours

Mon - Fri | 09:00 - 16:00
Sat - Sun | INCHIS

Useful links

  • Contact us
  • Services
  • Privacy Policy
  • Terms and conditions
  • ANPC

©2023 DEVSNIT - Simple solutions, complex connections - All rights reserved

Instant quote Contact us