- Place this proxy script on your webserver to be able to use your website's main domain as
server_container_urlin GTM. - You can also use your website's domain as 'Server container URLs' on the Container Settings so you can preview requests without cross-domain cookie problems.
This is best practice according to Google.
graph TD
Browser@{ shape: "stadium", label: "Visitor's Browser" }
subgraph AppServer [https\://www\.yourdomain\.com]
direction TB
Website_Content@{shape: "document", label: Website Content}
SsGtmProxy[ss-gtm-proxy]
end
Browser -- request --> Website_Content;
Website_Content -- HTML --> Browser;
Browser -- GTM requests
/g/*
/gtag/*
/gtm/*
--> SsGtmProxy;
SsGtmProxy -- forwards request --> Https_Proxy;
subgraph GtmServer [https\://gtm.anydomain.com/prefix]
Https_Proxy[HTTPS Proxy]
direction TB
Https_Proxy -- /prefix/g/*
/prefix/gtag/* --> GtmServerContainer[GTM Server Container];
Https_Proxy -- /prefix/gtm/* --> GtmPreviewContainer[GTM Preview Container];
end
%% Style definition for highlighting
style SsGtmProxy stroke-width:4px
- PHP 7.4 or greater
- Composer 2.x
- Apache 2.4 or greater
If you are not running Apache, you will need to manually implement the contents of the .htaccess in your webserver.
- You need to run the 2 GTM Docker containers on your own server
- one for the GTM Server
- one for the GTM Preview Server
- These Docker containers should have a public available HTTPS proxy (Caddy/HAProxy/Nginx) in front, with a public available URL with a working certificate.
- In the examples we use
https://gtm-docker-server.domain.com/prefixas the URL of this proxy.
- In the examples we use
- URL mappings in the container proxy server should be like this:
| Incomming URL | Edit Path | Pass to Container | Container Port |
|---|---|---|---|
| /prefix/gtm/* | Strip /prefix | GTM Preview Server | 8080 |
| /prefix/g/* | Strip /prefix | GTM Server | 8080 |
| /prefix/g/gtm.js | Strip /prefix/g | GTM Server | 8080 |
| /prefix/g/ns.html | Strip /prefix/g | GTM Server | 8080 |
| /prefix/gtag/* | Strip /prefix | GTM Server | 8080 |
| /prefix/* | Strip /prefix | GTM Server | 8080 |
-
Test the installation:
-
These URLs will return a 400 error until you complete step 6
- https://gtm-docker-server.domain.com/prefix/g/gtm.js
- https://gtm-docker-server.domain.com/prefix/g/ns.html
- these URLs also need the correct
?id=GTM-[WEBCONTAINER]parameter to work
Clone this repository, and execute the installation steps:
cd ~/httpdocs
git clone https://github.com/deovero/ss-gtm-proxy gtm
ln -sn gtm g
ln -sn gtm gtag
ln -sn gtm _
cd gtm
cp config.example.php config.php
composer install
- Update
~/httpdocs/gtm/config.phpwith the paths to your GTM server, for examplehttps://gtm-docker-server.domain.com/prefix
- Visit https://yourwebsite.com/gtm/healthy, this should also show
ok - Visit https://yourwebsite.com/g/healthy, this should show
ok - Visit https://yourwebsite.com/gtag/healthy, this should show
ok
I assume you already know how Server Side GTM works.
- On your GTM Server Container go to 'Admin' in the top menu, then to 'Container Settings'
- In the 'Server container URLs' field add your website URL, for example
https://yourwebsite.comas the only item - Submit/Publish the Server Container
- Wait a couple of minutes for the changes to be applied by the Docker container
- In the 'Server container URLs' field add your website URL, for example
- On your GTM Web Container a Tag of type 'Google Tag' must exist, containing your Analytics Tag ID
- On this Tag set the configuration parameter
server_container_urlto your website URL, for examplehttps://yourwebsite.com - This tag should have a trigger on 'Initialisation - All Pages'
- Submit/Publish the Web Container, or use Preview Mode
- On this Tag set the configuration parameter
- If you open PREVIEW on the GTM Server Container, leave this tab open
- Visit your website in the same browser on another tab, you should see the request on the preview page.
- Check your Google Analytics Realtime report to see if the hits are coming in.
To prevent any cross domain traffic you can serve the GTM JavaScript from your own domain.
- On your GTM Server Container add the 'Google Tag Manager: Web Container' Client
- Allow to serve
gtm.jsfor the ID of your GTM Web Container
- Allow to serve
- On your GTM Server Container edit the GT4 Client
- Enable 'Default gtag.js paths for specific IDs'
- Fill in the measurement ID of your Analytics, this looks like G-XXXXXXXXXX
- On these two settings only enable Region Specific settings if either:
- Your website is behind one of the proxies that provide region information like Cloudflare.
- You have manually provided the geo location headers
- Publish the Server Container
- Wait a couple of minutes for the changes to be applied by the Docker container
- Press the [Preview] button on the GTM Server Container management page
- Test the installation: https://gtm-docker-server.domain.com/prefix/gtm.js?id=GTM-[WEBCONTAINER] this should display the GTM JavaScript. If you are using Region Specific settings, this may fail if the
gtm-docker-server.domain.comis not passing GeoIP information. You should be able see the request on the Preview page. - Test the proxy: https://yourwebsite.com/g/gtm.js?id=GTM-[WEBCONTAINER]
- On your website source code where GTM is loaded, you should replace:
https://www.googletagmanager.com/gtm.jsbyhttps://yourwebsite.com/g/gtm.jshttps://www.googletagmanager.com/ns.htmlbyhttps://yourwebsite.com/g/ns.html
- Test your website
- Test if the tags added to the GTM Server Container work, like Google Analytics using the Realtime overview
- To use GT4 tracking server-side, you need to add the 'Google Analytics GA4' tag on the Server Container with a trigger on All Pages
- Created by DeoVero BV / Jeroen Vermeulen
- Thanks to @jenssegers for his work on PHP Proxy script which does the heavy lifting.