Skip to content

Instantly share code, notes, and snippets.

@sergejmueller
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergejmueller/d7cb47220cc3b3a6dbfd to your computer and use it in GitHub Desktop.
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
)
)
)
);
@macx
Copy link

macx commented Jun 6, 2014

Warum ein file_get_contents, wenn du die Rückgabewerte nicht benutzt? Wäre da ein CURL-Request nicht zweckdienlicher?

@sergejmueller
Copy link
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