2

Google Analytics appears to have a lot to offer me but I am concerned about the increasing use of extensions to limit tracking. I do not want an excessive amount of data but I would still like to track visitors who are otherwise privacy conscious and using extensions to limit these external things. For example to determine conversions.

Can I track visitors in Google Analytics using code on the backend instead of using the external javascript tracking?

2 Answers 2

2

No. Things just work differently on the back-end than the front-end.

There are statistic packages for the back-end which operate on log daily. You can also add code to the backend to accumulate statistics live but that may cause a heavy workload on the server.

The key point is that data that is available on the front-end is not the same as that which is available on the back-end. The back-end sees every requests, more so than analytics since it will catch Javascript being disabled, analytics blockers and bot traffic too.

Javascript on the front end has access to much more, including event handlers to know which link is clicked and other user behavior.

3
  • While I gather there are some differences I think the basics could possibly be handled on the server side. Why can't I pass Google details about URL, IP, and some UUID? Of course I won't be able to get all the fancy details that javascript might have access to. That's not really what I am interested in. I want more something like AWstats but with conversion tracking.
    – user86365
    Feb 9, 2018 at 3:31
  • @Matt - Yes, you can forward quite a bit of state and create a session even to track the user. As I said, it is not the same but you can get pretty far if you put effort into it.
    – Itai
    Feb 9, 2018 at 3:48
  • but the problem is that blockers filter request by their black url list, couldn't be possible that instead of the url pointing to google analytics, instead point to my own server?, of course google could also offer a backend configuration to handle the requests, it could even be possible not pointing my own server but instead the dns could be configure so the url could be target google servers Mar 7, 2019 at 15:05
1

I use server-side Google Analytics on my site, by using CURL to 'post' the info from a site visitor. Code similar to this works (with your GA codes, etc):

$url =  "{$_SERVER['REQUEST_URI']}";
if (strstr($url,'/test/')) {return "200";}  //no analytics for test area

$thecookie = ""; // set a cookie value
if (strlen($thecookie) == 0) { // if we can't create a cookie, use the sessionid
    $thecookie = session_id();
}
$pagename = $_SERVER['REQUEST_URI'];
$data = array(
    'v' => 1,
    'tid' => 'UA-xxxxxxxx-1', // site GA id
    'cid' => $thecookie, // customer ID (cookie or session id)
    't' => 'pageview', // type of access
    'dp' => $_SERVER['REQUEST_URI'], // page name
    'aip' => '1', //anonymize the IP address
    'uip' => $_SERVER['REMOTE_ADDR'], // used for geo data in analytics
    'ds' => 'web',  // datasource, optional, set to 'web'
    'dr' => $_SERVER['HTTP_REFERER'],       // referring page
    'z' => guid() ,     // cache buster, MUST BE LAST parameter!, a random guid-type value

);
$fields_string = http_build_query($data);
// uncomment next if you want to see the query sent
// echo "Sending ... " . $fields_string . "<br>";

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, utf8_encode($fields_string));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, "https://ssl.google-analytics.com/collect");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $thecookie);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
if (curl_errno($ch)) {
    // this would be your first hint that something went wrong
    // change to whatever you want to happen if CURL error
    // die('Couldn\'t send request: ' . curl_error($ch));
} else {
    // check the HTTP status code of the request
    $resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    //echo " curl results = $resultStatus <br>"; die("1259");
    if ($resultStatus == 200) {
        // everything went better than expected
        // uncomment if you want to see the response code
        // echo "Server GA OK; response = 200<br>";
    } else {
    // the request did not complete as expected. common errors are 4xx
        // (not found, bad request, etc.) and 5xx (usually concerning
        // errors/exceptions in the remote script execution)
        // die('Server GA Request failed: HTTP status code: ' . $resultStatus);
    }
}
curl_close($ch);
// uncomment next if you want to see all CURL respone values
//echo "<pre>";print_r($info);echo "</pre><hr>";
//echo "response = " . $response . "<br>";die();

This is my code, which I also published on my "SecurityDawg" site (www.securitydawg.com).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.