12

I tried to use google analytics to track some more custom data. So I thought I use the events. The following is the code I tried where I replaced the uuid and user agent:

<?php
function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}

$data = array(
'v' => 1,
'tid' => 'UA-********-**',
'cid' => gen_uuid(),
't' => 'event'
);


$data['ec'] = "category";
$data['ea'] = "product";
$data['el'] = "element";
$data['ev'] = "34";


$url = 'http://www.google-analytics.com/collect';
$content = http_build_query($data);
$content = utf8_encode($content);
$user_agent = 'Example/1.0 (http://example.com/)';


$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url);
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_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_exec($ch);
curl_close($ch);
?>

Am I missing something?

user2693017
  • 1,750
  • 6
  • 24
  • 50
  • Why do you think `am i missing something`? – Sabuj Hassan Mar 29 '14 at 14:36
  • because it doesn´t seem to work. or at least I don´t get any events logged in google analytics. – user2693017 Mar 29 '14 at 14:42
  • 1
    Can you add some examples of the final value for `$data`? In the end that's what matters when you are dealing with the measurement protocol – Eduardo Apr 08 '14 at 21:21
  • this should help http://www.stumiller.me/implementing-google-analytics-measurement-protocol-in-php-and-wordpress/ may be similar to what you're trying. – Suketu Apr 09 '14 at 12:03
  • Remember that Google analytics pet default only gives you data up untill yesterday. You have to choose your interval and make sure to specifically include today's date. – Atheist Feb 18 '15 at 22:36

2 Answers2

6

I've tested your code and it works.

But to help you debug this, change the /collect path to /debug/collect and Google will validate your hit.

Try:

<?php
function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}

$data = array(
'v' => 1,
'tid' => 'UA-1111111-1',
'cid' => gen_uuid(),
't' => 'event'
);


$data['ec'] = "category";
$data['ea'] = "product";
$data['el'] = "element";
$data['ev'] = "34";


$url = 'https://www.google-analytics.com/debug/collect';
$content = http_build_query($data);
$content = utf8_encode($content);
$user_agent = 'Example/1.0 (http://example.com/)';


$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url);
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_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_exec($ch);
curl_close($ch);
?>

And, make sure to go to the Real Time reports to check if your hits are being processed.

Dzmitry Kulahin
  • 1,726
  • 2
  • 17
  • 21
Leonardo Naressi
  • 313
  • 4
  • 14
  • Couldn't people add data to somebody else reports with this? If you knew their UA. – Gary Carlyle Cook Dec 06 '17 at 21:16
  • 1
    Sure Gary! It's important to regularly check your hostnames report and/or implement some hit filters to guarantee that your official view/report only contains your desired data collection. – Leonardo Naressi Dec 08 '17 at 14:27
  • I've tried this code and it seems ok in debug mode, but it returns a 500 Error when executed on https://www.google-analytics.com/collect. What should I check? – kiks73 Sep 25 '18 at 13:13
  • If you're using Measurement Protocol (`/mp/collect`) and you want to debug your request you should send requests to Measurement Protocol Validation Server (`/debug/mp/collect`). Details: https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events?client_type=gtag#sending_events_for_validation – Dzmitry Kulahin Feb 09 '23 at 11:55
1

You can try this

        <?php
        function gen_uuid() {
        return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
        mt_rand( 0, 0xffff ),
        mt_rand( 0, 0x0fff ) | 0x4000,
        mt_rand( 0, 0x3fff ) | 0x8000,
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
        );
        }

        $data = array(
        'v' => 1,
        'tid' => 'UA-********-**',
        'cid' => gen_uuid(),
        't' => 'event'
        );


        $data['ec'] = "category";
        $data['ea'] = "product";
        $data['el'] = "element";
        $data['ev'] = "34";


        $url = 'http://www.google-analytics.com/collect';
        $content = http_build_query($data);
        $content = utf8_encode($content);
        $user_agent = 'Example/1.0 (http://example.com/)';


        $ch = curl_init();
        curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
        curl_setopt($ch, CURLOPT_URL, $url);
        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_POST, TRUE);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
        curl_exec($ch);
        curl_close($ch);
        ?>

I think this may works

saleem ahmed
  • 326
  • 1
  • 5
  • 27