heyday / silverstripe-googleanalytics
A thin SilverStripe helper for the php-ga library
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 24
Forks: 1
Open Issues: 0
Type:silverstripe-module
Requires
- camspiers/autoloader-composer-silverstripe: ~1.0
- composer/installers: ~1.0
- unitedprototype/php-ga: dev-master
Requires (Dev)
- phpunit/phpunit: ~3.7
README
A thin SilverStripe helper for the php-ga library
Installation (with composer)
$ composer require heyday/silverstripe-googleanalytics:~0.1
Usage
Config
SSGoogleAnalytics::setDomain('website.com'); SSGoogleAnalytics::setTrackingCode('UA-11111111-1'); SSGoogleAnalytics::setLoggingCallback(function ($request, $response) { mail( 'email@email.com', 'Analytics log - dev', $request, "Content-type: text/html\nFrom: email@email.com" ); });
Track Page View
use UnitedPrototype\GoogleAnalytics; $analytics = new SSGoogleAnalytics(); $pageView = new GoogleAnalytics\Page('/home'); $analytics->trackPageview($pageView);
Track Event
use UnitedPrototype\GoogleAnalytics; $analytics = new SSGoogleAnalytics(); $event = new GoogleAnalytics\Event('Category', 'Action', 'Label', 'Value', 'NonInteraction'); $analytics->trackEvent($event);
Track Transaction
use UnitedPrototype\GoogleAnalytics; $analytics = new SSGoogleAnalytics(); $transactionAnalytics = new GoogleAnalytics\Transaction(); $transactionAnalytics->setOrderId($orderID); $transactionAnalytics->setAffiliation($affiliation); $transactionAnalytics->setTotal($total); $transactionAnalytics->setTax($taxTotal); $transactionAnalytics->setShipping($shippingTotal); $transactionAnalytics->setCity($city); $transactionAnalytics->setRegion($region); $transactionAnalytics->setCountry($country); $items = array(); foreach ($products as $product) { $items[$product->ProductCode] = new GoogleAnalytics\Item(); $items[$product->ProductCode]->setOrderId($orderID); $items[$product->ProductCode]->setSku($product->ProductCode); $items[$product->ProductCode]->setName($product->Title); $items[$product->ProductCode]->setVariation($product->Category); $items[$product->ProductCode]->setPrice($product->Price); $items[$product->ProductCode]->setQuantity($product->Quantity); $items[$product->ProductCode]->validate(); $transactionAnalytics->addItem($items[$product->ProductCode]); } $analytics->trackTransaction($transactionAnalytics);
Unit testing
$ composer install --dev $ vendor/bin/phpunit