Skip to content

Instantly share code, notes, and snippets.

@sergejmueller
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save sergejmueller/d7cb47220cc3b3a6dbfd to your computer and use it in GitHub Desktop.

Select an option

Save sergejmueller/d7cb47220cc3b3a6dbfd to your computer and use it in GitHub Desktop.
Tracking der Bestellungen in Google Analytics (GA) mithilfe von PHP und Measurement Protocol. Weitere Informationen: https://developers.google.com/analytics/devguides/collection/protocol/v1/
<?php
// GA Tracking ID
$tid = 'UA-XXXXXXX-XX';
// Anonymous Client ID
$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 )
);
// Track order
file_get_contents(
sprintf(
'https://ssl.google-analytics.com/collect?payload_data&%s',
http_build_query(
array(
'v' => 1,
'tid' => $tid,
'cid' => $cid,
'cu' => 'EUR',
'tr' => $order_cost,
't' => 'transaction',
'ti' => $txn_id
)
)
)
);
// Track item
file_get_contents(
sprintf(
'https://ssl.google-analytics.com/collect?payload_data&%s',
http_build_query(
array(
'v' => 1,
'tid' => $tid,
'cid' => $cid,
'cu' => 'EUR',
'in' => 'Product',
'ip' => $order_cost,
't' => 'item',
'iq' => 1,
'ti' => $txn_id
)
)
)
);
@sergejmueller
Copy link
Copy Markdown
Author

file_get_contents ist übersichtlicher, weil nur ein Befehl. Klar, kann man auch cURL nehmen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment