Skip to content

Latest commit

 

History

History
83 lines (66 loc) · 3.54 KB

README.md

File metadata and controls

83 lines (66 loc) · 3.54 KB

GA-proxy 🕵️‍

A simple proxy server for Google Analytics that will bypass most content blockers, so you get non-truncated insights.

Disclaimer : You still have to comply with the GDPR by displaying a consent banner to your users.

Requirements

GA-proxy is built with Lumen 8. Therefore, you'll need the same requirements as Lumen does :

  • PHP >= 7.3
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension

Installation

git clone https://github.com/anto2oo/ga-proxy
cd ga-proxy
composer install

cp .env.example .env
# Please update environement values according to your needs

Usage

In any case, you just need to replace the script src attribute in the snippets generated by Google Analytics.

With gtag.js (recommended by Google)

<script async src="https://<YOUR-SERVER>/tag/js?id=<GOOGLE-ANALYTICS-ID>"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', '<GOOGLE-ANALYTICS-ID>');
</script>

Replace /tag/js according to the scrambled value you set in the .env file (GA_TAG_NAME).

With analytics.js

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://<YOUR-SERVER>/analytics.js','ga');

    ga('create', '<GOOGLE-ANALYTICS-ID>', 'auto');
    ga('send', 'pageview');
</script>

Replace /analytics.js according to the scrambled value you set in the .env file (GA_SCRIPT_NAME).

You can confirm that the proxy is working properly by filling the GA_DEMO_ID environment value with any property ID and browsing /test/analytics or /test/gtag.

How does it work

Content blockers usually prevent connections to domains like google-analytics.com, but also detect patterns like the collection URL which starts with /collect.
This proxy servers scrambles both the script and collection URLs so it is not blocked by content blockers.

Analytics tags

When you fetch an analytics tag from the proxy server, it first gets a fresh version from Google, then replaces the following values :

Searched value Replaced value
www.google-analytics.com Request hostname
www.googletagmanager.com Request hostname
analytics.js GA_SCRIPT_NAME
gtag/js GA_TAG_NAME
/collect GA_COLLECT_ENDPOINT
/j/collect GA_COLLECT_ENDPOINT
/r/collect GA_COLLECT_ENDPOINT
So the script only make requests to your server and not directly connect to the Google Analytics server.
Some connections to Doubleclick may still occur if you are testing your ads performance.

Data collection

Because some blockers recognize the pattern /collect and block it, the collection endpoint also has to be scrambled. It is automatically set to GA_COLLECT_ENDPOINT (from the environment), so don't forget to change it. Once changed, it will be automatically updated in the analytics tags.
If you change it in production, don't forget bust the cache for any tag in use.

Technologies used