PHP code example of 8ctopus / php-ga-measurement-protocol
1. Go to this page and download the library: Download 8ctopus/php-ga-measurement-protocol library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
8ctopus / php-ga-measurement-protocol example snippets
use TheIconic\Tracking\GoogleAnalytics\Analytics;
// Instantiate the Analytics object
// optionally pass TRUE in the constructor if you want to connect using HTTPS
$analytics = new Analytics(true);
// Build the GA hit using the Analytics class methods
// they should Autocomplete if you use a PHP IDE
$analytics
->setProtocolVersion('1')
->setTrackingId('UA-26293728-11')
->setClientId('12345678')
->setDocumentPath('/mypage')
->setIpOverride("202.126.106.175");
// When you finish bulding the payload send a hit (such as an pageview or event)
$analytics->sendPageview();
// Look at the parameter names in Google official docs at
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
$analytics->set<ParameterName>('my_value');
// Get any parameter by its name
// Look at the parameter names in Google official docs at
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
$analytics->get<ParameterName>();
use TheIconic\Tracking\GoogleAnalytics\Analytics;
$analytics = new Analytics();
// Build the order data programmatically, including each order product in the payload
// Take notice, if you want GA reports to tie this event with previous user actions
// you must get and set the same ClientId from the GA Cookie
// First, general and tTransactionId(1667) // transaction id. r id
->setItemCategory($cartProduct->category) // item variation: category, size, color etc.
->setItemPrice($cartProduct->price)
->setItemQuantity($cartProduct->qty)
// make the 'item' hit
->sendItem();
}
use TheIconic\Tracking\GoogleAnalytics\Analytics;
$analytics = new Analytics();
// Build the order data programmatically, including each order product in the payload
// Take notice, if you want GA reports to tie this event with previous user actions
// you must get and set the same ClientId from the GA Cookie
// First, general and ponCode('MY_COUPON');
// Include a product
$productData1 = [
'sku' => 'AAAA-6666',
'name' => 'Test Product 2',
'brand' => 'Test Brand 2',
'category' => 'Test Category 3/Test Category 4',
'variant' => 'yellow',
'price' => 50.00,
'quantity' => 1,
'coupon_code' => 'TEST 2',
'position' => 2
];
$analytics->addProduct($productData1);
// You can
$analytics = new Analytics(false, false);
$analytics
->setProtocolVersion('1')
->setTrackingId('UA-xxxxxx-x')
->setClientId('xxxxxx.xxxxxx');
foreach(range(0, 19) as $i) {
$analytics = $analytics
->setDocumentPath("/mypage$i")
->enqueuePageview(); //enqueue url without pushing
}
$analytics->sendEnqueuedHits(); //push 20 pageviews in a single request and empties the queue
$analytics = new Analytics(false, false);
$analytics
->setProtocolVersion('1')
->setTrackingId('UA-xxxxxx-x')
->setClientId('xxxxxx.xxxxxx');
foreach(range(0, 5) as $i) {
$analytics = $analytics
->setDocumentPath("/mypage$i")
->enqueuePageview(); //enqueue url without pushing
}
$analytics->emptyQueue(); // empty queue, allows to enqueue 20 hits again
foreach(range(1, 20) as $i) {
$analytics = $analytics
->setDocumentPath("/mypage$i")
->enqueuePageview(); //enqueue url without pushing
}
$analytics->sendEnqueuedHits(); //push 20 pageviews in a single request and empties the queue
// Make sure AsyncRequest is set to false (it defaults to false)
$response = $analytics
->setDebug(true)
->sendPageview();
$debugResponse = $response->getDebugResponse();
// The debug response is an associative array, you could use print_r to view its contents
print_r($debugResponse);
// Instantiate the Analytics object by passing the second parameter in the constructor as TRUE
$analytics = new Analytics(true, true);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.