Payment tracking problem

958 views
Skip to first unread message

Antonio Pellegrino

unread,
Dec 6, 2012, 3:12:11 AM12/6/12
to google-analytics-m...@googlegroups.com
I'm trying to track payments of a callback in a virtual pos payment method in prestashop (just like paypal IPN)
here's my code when payment gets OK:


function make_google_request($arr) {


    $str = http_build_query($arr);

    //print "http://www.google-analytics.com/collect?$str";

    return file_get_contents("http://www.google-analytics.com/collect?$str");

}

        $order = array(
            'v' => 1,
            'tid' => 'UA-*******-3',
            't' => 'transaction',
            'ti' =>    $iwsmile->currentOrder,
            'tr' => $total,
            'ts' => $shipping,
            'tt' => $tax
        );
       
        make_google_request($order);


       
                foreach ($orderDetails as $d){
   
                       
                    $price = Tools::convertPrice($d['product_price'],(int)$currency,true);
                    $price = round($price+ (($price/100)*$d['tax_rate']),2);
                    $item = array(
                        'v' => 1,
                        'tid' => 'UA-*******-3',
                        't' => 'item',
                        'ti' => $iwsmile->currentOrder,
                        'ip' => $price,
                        'iq' => $d['product_quantity'],
                        'ic' => $d['product_id'],
                        'in' => $d['product_name']
                    );
                   
                    make_google_request($order);


                }

Function returns a blank gif but when i get back to my analytics panel, i see no transaction/item tracking
What am i doing wrong? Is any way to debug it?
Thanks

Willem Paling

unread,
Dec 6, 2012, 5:20:54 PM12/6/12
to google-analytics-m...@googlegroups.com
Can you log the URL that it's requesting?

Might be worth trying to work backwards...put the URL together manually, make sure that is logging data that you can see in the analytics panel, then make sure that your PHP script is using a similarly formatted URL.

Matt Clarke

unread,
Dec 12, 2012, 8:42:05 AM12/12/12
to google-analytics-m...@googlegroups.com
Hi Antonio,
I could be wrong, but I don't think file_get_contents() POSTs the data unless you explicitly set this with a context. Do your error logs show anything?

If you use Curl you'll be able to POST the data to the API and get the HTTP status code back in your headers. If you find a 200 in there, you'll know your response was definitely successful. 

Cheers,
Matt

Antonio Pellegrino

unread,
Dec 27, 2012, 6:35:04 AM12/27/12
to google-analytics-m...@googlegroups.com
thanks for helping

I changed my request to post and added the cid parameter
my func looks like this now:

    function getUuid() {
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 )
);

}

function make_google_request($arr) {


    $str = http_build_query($arr);

    //print "http://www.google-analytics.com/collect?$str";
   
    $postdata = http_build_query($arr);

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);

    $result = file_get_contents('http://www.google-analytics.com/collect', false, $context);
   
    file_put_contents("/var/www/***/glogs.txt", $postdata . "\r\n\r\n", FILE_APPEND);

    file_put_contents("/var/www/***/glogs.txt", $result . "\r\n\r\n", FILE_APPEND);
   
    file_put_contents("/var/www/***/glogs.txt",json_encode($http_response_header) . "\r\n\r\n",FILE_APPEND);

}

i logged everything in the txt file and i get 200 status code but i can't see any ecommerce tracking in my analytics panel

here is what my glogs.txt reports:

v=1&tid=UA-29822476-1&t=transaction&ti=79&tr=26.9&ts=7.9&tt=0&cid=78c6dc4a-65e3-4f5e-b13d-8f96525d062f

GIF89a �����, D ;

["HTTP\/1.0 200 OK","Date: Thu, 27 Dec 2012 11:29:46 GMT","Content-Length: 35","X-Content-Type-Options: nosniff","Pragma: no-cache","Cache-Control: private, no-cache, no-cache=Set-Cookie, proxy-revalidate","Expires: Mon, 07 Aug 1995 23:30:00 GMT","Last-Modified: Sun, 17 May 1998 03:00:00 GMT","Content-Type: image\/gif","Access-Control-Allow-Origin: *","Server: GFE\/2.0"]

v=1&tid=UA-29822476-1&t=transaction&ti=79&tr=26.9&ts=7.9&tt=0&cid=78c6dc4a-65e3-4f5e-b13d-8f96525d062f

GIF89a �����, D ;

["HTTP\/1.0 200 OK","Date: Thu, 27 Dec 2012 11:29:46 GMT","Content-Length: 35","X-Content-Type-Options: nosniff","Pragma: no-cache","Cache-Control: private, no-cache, no-cache=Set-Cookie, proxy-revalidate","Expires: Mon, 07 Aug 1995 23:30:00 GMT","Last-Modified: Sun, 17 May 1998 03:00:00 GMT","Content-Type: image\/gif","Access-Control-Allow-Origin: *","Server: GFE\/2.0"]

David Vallejo

unread,
Jan 8, 2013, 7:34:22 PM1/8/13
to google-analytics-m...@googlegroups.com
file_get_contents does not send the user agent by default , and Google Analytics Measuring protocol specifies that wihout it won't be computed.

From Reference Page:
  • user_agent_string – Is a formatted user agent string that is used to compute the following dimensions: browser, platform, and mobile capabilities.

    If this value is not set, the data above will not be computed.

Try with this context to see if it works.

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n".
                          "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.107 Safari/534.13",

        'content' => $postdata
    )
);

If your hosting/server allows you to use ini_set function, you could this line too:
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.107 Safari/534.13');

Hope it helps.
David
Reply all
Reply to author
Forward
0 new messages