Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics Measurement Protocol

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?

like image 952
user2693017 Avatar asked Mar 29 '14 14:03

user2693017


1 Answers

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

like image 149
saleem ahmed Avatar answered Oct 06 '22 03:10

saleem ahmed