user3863696
user3863696

Reputation: 35

Google Analitics : Send PageViews or Event with PHP

I have an website who already use Google Analytics, but it' didn't send page view for Ajax action (a Dynamical search engine for exemple) i don't find an API to send it with PHP by server.

Upvotes: 3

Views: 320

Answers (1)

ins0
ins0

Reputation: 3928

For your task look at the Google Measurement Protocol https://developers.google.com/analytics/devguides/collection/protocol/v1/?hl=de

Now i point to my Github project - maybe it is useful for your task.

https://github.com/ins0/google-measurement-php-client

it is a PHP client library to communicate to Google Analytics from server-side.

// autoloader
require_once( dirname(__FILE__) . '/../src/Racecore/GATracking/Autoloader.php');
Racecore\GATracking\Autoloader::register(dirname(__FILE__).'/../src/');

// init tracking
$tracking = new \Racecore\GATracking\GATracking('UA-XXXXXXXX-X',false);

// optional when not setting the client id by constructor
$tracking->setAccountID('UA-XXXXXXXX-X');

/**
 * Page Tacking
 */
$page = new \Racecore\GATracking\Tracking\Page();
$page->setDocumentPath('/test/pageview/blub.jpg');
$page->setDocumentTitle('Test Image Title');

$tracking->addTracking($page);

// Do the Job!
Try {
    $tracking->send();
    echo 'success';

} Catch (Exception $e) {
    echo 'Error: ' . $e->getMessage() . '<br />' . "\r\n";
    echo 'Type: ' . get_class($e);
}

otherwise you can use other great repositorys, look here

https://github.com/search?q=google-measurement&source=cc

Upvotes: 1

Related Questions