Last active
September 30, 2016 21:32
-
-
Save mbaersch/e825eec60a1fc90dc2bb to your computer and use it in GitHub Desktop.
Serverside tracking of bots and users using PHP and Google Analytics Measurment Protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* gms.serverside-analytics.php | v0.4 | |
www.gandke.de | |
Copyright (c) 2015 Markus Baersch (@mbaersch) | |
Licensed under MIT license. | |
*/ | |
/*********************************** | |
VARIANT A: TRACK BOTS AND REAL USERS | |
************************************/ | |
function track_hit($pgTitle) { | |
function send_ga_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, 50); | |
curl_exec($ch); | |
cURL_close($ch); | |
return true ; | |
} | |
function get_client_id() { | |
try { | |
$cv = explode('.', $_COOKIE["_ga"]); | |
return $cv[2].".".$cv[3] ; | |
} catch(Exception $e) { | |
return "" ; | |
} | |
} | |
//ENTER PROPERTY-ID HERE | |
$ua = "UA-xxxxxx-1" ; | |
//DEFINE CONSTANT CIDS, NAMES, PATTERNS FOR DIFFERENT BOTS | |
$segments = array( | |
"770" => "Google::(Googlebot|Mediapartners-Google|AdsBot-Google)", | |
"771" => "Bing::(bingbot)", | |
"772" => "Baidu::(BaiDuSpider|Baiduspider)", | |
"773" => "Yandex::(YandexBot)", | |
"779" => "Other::(bot)", | |
); | |
$profile = "" ; | |
$agent = urlencode($_SERVER['HTTP_USER_AGENT']) ; | |
$cid = get_client_id() ; | |
if (strlen($cid) > 2) $profile = "Tracked Users" ; | |
if (empty($profile)) { | |
foreach ($segments as $id => $segment) { | |
$param = explode("::", $segment); | |
if (preg_match('/'.$param[1].'/', $agent)) { | |
$profile = "Bot-Traffic (".$param[0].")" ; | |
$cid = $id ; | |
break ; | |
} | |
} | |
} | |
if (empty($profile)) { | |
$cid = "555" ; | |
$profile = "Unknown" ; | |
} | |
$referer = urlencode($_SERVER['HTTP_REFERER']) ; | |
$pgurl = urlencode($_SERVER['PHP_SELF']) ; | |
$pgttl = urlencode($pgTitle) ; | |
$profile = urlencode($profile) ; | |
$collect_url = "https://www.google-analytics.com/collect?v=1&tid=$ua&cid=$cid&t=pageview&". | |
"dp=$pgurl&dt=$pgttl&cs=Serverside+Tracking&cm=$profile&ua=$agent&dr=$referer" ; | |
send_ga_hit($collect_url) ; | |
} | |
/*************************** | |
VARIANT B: TRACK BOTS ONLY | |
****************************/ | |
function track_bot_hit($pgTitle) { | |
function send_ga_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, 50); | |
curl_exec($ch); | |
cURL_close($ch); | |
return true ; | |
} | |
//ENTER PROPERTY-ID HERE | |
$ua = "UA-xxxxxx-1" ; | |
//DEFINE CONSTANT CIDS, NAMES, PATTERNS FOR DIFFERENT BOTS | |
$segments = array( | |
"770" => "Google::(Googlebot|Mediapartners-Google|AdsBot-Google)", | |
"771" => "Bing::(bingbot)", | |
"772" => "Baidu::(BaiDuSpider|Baiduspider)", | |
"773" => "Yandex::(YandexBot)", | |
"779" => "Other::(bot)", //KILL THIS ENTRY TO TRACK ONLY KNOWN BOTS | |
); | |
$profile = "" ; | |
$agent = urlencode($_SERVER['HTTP_USER_AGENT']) ; | |
foreach ($segments as $id => $segment) { | |
$param = explode("::", $segment); | |
if (preg_match('/'.$param[1].'/', $agent)) { | |
$profile = "Bot-Traffic (".$param[0].")" ; | |
$cid = $id ; | |
break ; | |
} | |
} | |
if (!empty($profile)) { | |
$referer = urlencode($_SERVER['HTTP_REFERER']) ; | |
$pgurl = urlencode($_SERVER['PHP_SELF']) ; | |
$pgttl = urlencode($pgTitle) ; | |
$profile = urlencode($profile) ; | |
$collect_url = "https://www.google-analytics.com/collect?v=1&tid=$ua&cid=$cid&t=pageview&". | |
"dp=$pgurl&dt=$pgttl&cs=Serverside+Tracking&cm=$profile&ua=$agent&dr=$referer" ; | |
send_ga_hit($collect_url) ; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gms.serverside-analytics.php
Beispielcode zum Tracking von Besuchern und Bots (Variante A) bzw. ausschließlich Bots (Variante B) in Google Analytics mit Hilfe des Measurment Protocols
Einsatz
Je nach Wunsch kann entweder die Funktion
track_hit()
odertrack_bot_hit()
in das eigene PHP-basierende CMS bzw. Template implementiert werden, um Seitenaufrufe an ein Datenprofil in Google Analytics zu senden und dort anhand der Dimension "Medium" auszuwerten.Einstellungen
$ua
mit der gewünschten ID versehen werden.Weitere Hinweise zum Einsatz finden sich im Blog unter blog.markus-baersch.de.
Feedback ist jederzeit willkommen. Happy Tracking! ;)