Skip to content

Instantly share code, notes, and snippets.

@mbaersch
Last active July 7, 2022 14:48
Show Gist options
  • Save mbaersch/6590c060d05c00ca31a2f95c55aabb9e to your computer and use it in GitHub Desktop.
Save mbaersch/6590c060d05c00ca31a2f95c55aabb9e to your computer and use it in GitHub Desktop.
simple PHP proxy for Google Analytics hits
<?php
/******************************************************************************************************/
//send hits to GA- or GTM tag-server: use this file via transport_url
//more info: https://developers.google.com/tag-manager/serverside/send-data
/******************************************************************************************************/
//define destination for tracking hits - either on www.google-analytics.com
//or own tag server like https://trk.myserver.com/collect or
//https://gtm-xxxxxx-xxxx.xx.x.appspot.com/collect :
$endpoint = "https://www.google-analytics.com/collect";
/******************************************************************************************************/
function get_domain_from_host($h) {
$hst = explode(".", $h);
return $hst[count($hst)-2] . "." . $hst[count($hst)-1];
}
function send_hit($url) {
$ch = cURL_init($url);
cURL_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
cURL_setopt($ch, CURLOPT_FRESH_CONNECT, true);
cURL_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
//optional: send cookie values (if your own client on the tag
//server needs them - maybe increase timeout as well)
/*
curl_setopt($ch, CURLOPT_HEADER, 1);
$cookie_string = "" ;
foreach($_COOKIE as $key => $value) {
$cookie_string .= "$key=$value;";
}
curl_setopt($ch,CURLOPT_COOKIE, $cookie_string);
*/
curl_exec($ch);
//you might want to persist cookies here
//if your GTM tag server client sends cookies in the response
cURL_close($ch);
return true ;
}
/******************************************************************************************************/
$params = $_SERVER['QUERY_STRING'];
$call_from = strtolower(get_domain_from_host(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST)));
$this_domain = strtolower(get_domain_from_host($_SERVER['SERVER_NAME']));
//only accept requests from same domain - comment out if not needed or running on a different host
if ($call_from != $this_domain) $params = "";
if ($params != "") {
$collect_url = $endpoint."?".$params;
send_hit($collect_url);
header('Content-Type: image/gif');
echo(hex2bin('47494638396101000100900000ff000000000021f90405100000002c00000000010001000002020401003b'));
} else {
header('HTTP/1.0 403 Forbidden');
echo "No!";
}
?>
<?php
/******************************************************************************************************/
//loading of analytics.js - in a (too) simple way. do not use like this in a productive environment ;)
/******************************************************************************************************/
$scriptcode = file_get_contents('https://www.google-analytics.com/analytics.js');
header("Content-type: text/javascript");
header("cache-control: public, max-age=7200");
header("content-length: ".strlen($scriptcode));
echo $scriptcode;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment