Skip to content

Instantly share code, notes, and snippets.

@kerstvo
Created November 9, 2015 13:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerstvo/f6cd761fd928bc4a7d36 to your computer and use it in GitHub Desktop.
Save kerstvo/f6cd761fd928bc4a7d36 to your computer and use it in GitHub Desktop.
<?php
var_dump(gaSendRefund('2642236223'));
function gaSendRefund($transaction_id)
{
$url = 'https://www.google-analytics.com/collect';
$tid = 'UA-XXXXXXX-X';
$params = http_build_query(
array(
'v' => '1',
'tid' => $tid,
'cid' => 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)
),
't' => 'event',
'ec' => 'Ecommerce',
'ea' => 'Refund',
'ni' => 1,
'ti' => $transaction_id,
'pa' => 'refund'
)
);
if (false && function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
return curl_exec($ch);
} elseif (ini_get('allow_url_fopen')) {
$opts = array(
'http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $params
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context); // POST
// return file_get_contents($url.'?'.$params); // GET
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment