PHP code example of gaconnector / server-side-tracking

1. Go to this page and download the library: Download gaconnector/server-side-tracking 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/ */

    

gaconnector / server-side-tracking example snippets




use GaConnector\Tracking\GaConnector;

$gac = GaConnector::create([
    'apiKey'   => getenv('GAC_API_KEY'),   // gac_api_<accountId>_<secret>
    'baseUrl' => 'https://example.com/gac', // absolute public URL of the proxy mount
    // optional:
    // 'mode'            => 'auto',         // 'auto' (default) or 'consent'
    // 'debug'           => false,
    // 'iframeEnabled'   => true,
    // 'internalDomains' => ['shop.example.com'],
    // 'inlineContext'   => false,          // see "Server-side page context" below
]);

echo $gac->html();

echo $gac->settingsScript();   // cacheable: __gacSettings + __gacStatus
echo $gac->contextScript();    // per-request: __gacContext
echo $gac->scriptTag();        // the tracker <script> tag

$gac->serve();   // reads superglobals, routes, emits

use GaConnector\Tracking\Http\Request;

$response = $gac->proxy()->handlePageview(Request::fromGlobals());
$response->emit();

use GaConnector\Tracking\Exception\AccountVerificationException;
use GaConnector\Tracking\Exception\NoHttpTransportException;

try {
    $account = $gac->verifyAccount('example.com');
    // $account is a GaConnector\Tracking\Account value object:
    //   $account->accountId, $account->accountName, $account->email, $account->allowedDomains
    // allows() is true for an exact host or a subdomain of a registered domain
    // (e.g. www-staging.example.com when example.com is listed).
    $connected = $account->allows('example.com');
} catch (AccountVerificationException $e) {
    // $e->getStatus() is 401 (bad key), 403 (subscription lapsed), or 404 (unknown account)
} catch (NoHttpTransportException $e) {
    // neither curl nor sockets available on this host
}

use GaConnector\Tracking\GaConnector;

// bootstrap (once):
GaConnector::configure([
    'apiKey'   => getenv('GAC_API_KEY'),
    'baseUrl' => 'https://example.com/gac',
]);

// anywhere after:
echo GaConnector::html();                 // in a template
GaConnector::serve();                     // in the /gac/* controller
$account = GaConnector::verifyAccount('example.com');