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

0.1.3 2014-08-25 22:33 UTC

This package is auto-updated.

Last update: 2024-02-29 03:17:04 UTC


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