All Projects → theiconic → Php Ga Measurement Protocol

theiconic / Php Ga Measurement Protocol

Licence: mit
Send data to Google Analytics from the server using PHP. Implements GA measurement protocol.

Projects that are alternatives of or similar to Php Ga Measurement Protocol

svelte-google-analytics
Google Analytics component for Svelte
Stars: ✭ 41 (-92.69%)
Mutual labels:  tracking, analytics, google-analytics
Vue Analytics
Google Analytics plugin for Vue
Stars: ✭ 1,780 (+217.29%)
Mutual labels:  analytics, tracking, google-analytics
Aurelia Google Analytics
An Aurelia.io plugin that adds Google Analytics page tracking to your project.
Stars: ✭ 28 (-95.01%)
Mutual labels:  analytics, tracking, google-analytics
React Tracker
React specific tracking library, Track user interaction with minimal API!
Stars: ✭ 191 (-65.95%)
Mutual labels:  analytics, tracking, google-analytics
Gumshoe
A we analytics and event tracking sleuth JavaScript library
Stars: ✭ 39 (-93.05%)
Mutual labels:  analytics, tracking, google-analytics
Vue Gtag
Global Site Tag plugin for Vue (gtag.js)
Stars: ✭ 445 (-20.68%)
Mutual labels:  analytics, tracking, google-analytics
Blacklist
Curated and well-maintained hostfile to block ads, tracking, cryptomining, and more! Updated regularly. ⚡🔒
Stars: ✭ 492 (-12.3%)
Mutual labels:  analytics, tracking
aws-web-analytics
Privacy-focused alternative to Google Analytics on AWS Pinpoint
Stars: ✭ 45 (-91.98%)
Mutual labels:  analytics, google-analytics
wp-analytify
Google Analytics Dashboard Plugin For WordPress By Analytify
Stars: ✭ 20 (-96.43%)
Mutual labels:  analytics, google-analytics
Signal
Simple and beautiful open source Analytics 📊
Stars: ✭ 295 (-47.42%)
Mutual labels:  analytics, google-analytics
web-analytics-handbook
Handbook - Rendezvous between developers and web data
Stars: ✭ 23 (-95.9%)
Mutual labels:  analytics, google-analytics
Pirsch
Pirsch is a drop-in, server-side, no-cookie, and privacy-focused analytics solution for Go.
Stars: ✭ 257 (-54.19%)
Mutual labels:  analytics, tracking
Countly Server
Countly helps you get insights from your application. Available self-hosted or on private cloud.
Stars: ✭ 4,857 (+765.78%)
Mutual labels:  analytics, tracking
Stop.Google.Analytics.Ghost.Spam.HOWTO
How to stop Google Analytics "Ghost" Spam using a well curated list of spam referrer domains and web sites. Simple and easy to use with instructions for creating Segments in Google Analytics using our google-exclude files.
Stars: ✭ 21 (-96.26%)
Mutual labels:  analytics, google-analytics
Ganalytics
A tiny (312B) client-side module for tracking with Google Analytics
Stars: ✭ 491 (-12.48%)
Mutual labels:  analytics, google-analytics
Social-Media-Monitor
Automatically monitor and log fan counters from social media(Facebook Pages, Twitter, Instagram, YouTube, Google+, OneSignal, Alexa) using APIs to Google Spreadsheet. Very useful for website admins and social media managers.
Stars: ✭ 36 (-93.58%)
Mutual labels:  analytics, google-analytics
Laravel Gamp
📊 Laravel Google Analytics Measurement Protocol Package
Stars: ✭ 271 (-51.69%)
Mutual labels:  analytics, google-analytics
Legato
Google Analytics Reporting API Client for Ruby
Stars: ✭ 407 (-27.45%)
Mutual labels:  analytics, google-analytics
Staccato
Ruby library to perform server-side tracking into the official Google Analytics Measurement Protocol
Stars: ✭ 380 (-32.26%)
Mutual labels:  analytics, google-analytics
Tracklytics
✔️ Annotation based tracking handler with aspect oriented programming
Stars: ✭ 416 (-25.85%)
Mutual labels:  analytics, tracking

Google Analytics Measurement Protocol library for PHP

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads License Documentation Status

Description

Send data to Google Analytics from the server using PHP. This library fully implements GA measurement protocol so its possible to send any data that you would usually do from analytics.js on the client side. You can send data regarding the following parameters categories (Full List):

  • General
  • User
  • Session
  • Traffic Sources
  • System Info
  • Hit
  • Content Information
  • App Tracking
  • Event Tracking
  • E-Commerce
  • Enhanced E-Commerce
  • Social Interactions
  • Timing
  • Exceptions
  • Custom Dimensions / Metrics
  • Content Experiments
  • Content Grouping

Sponsor

If you use this library, please consider to ❤️ Sponsor. Reaching the goal you can see in my sponsor page will allow me to focus on:

  • Upgrading the library to support PHP version >=7.2
  • Keep dependencies up to date
  • Fix any critical bugs

Installation

Use Composer to install this package.

If you are using PHP 5.5 or above and Guzzle 6 then:

{
    "require": {
        "theiconic/php-ga-measurement-protocol": "^2.0"
    }
}

Or if you are using PHP 5.4 or above and Guzzle 5 then:

{
    "require": {
        "theiconic/php-ga-measurement-protocol": "^1.1"
    }
}

Take notice v1 is no longer maintained and lacks newer features (such as Debug and Hit validation), you are encourage to update to v2.

Integrations

You can use this package on its own, or use a convenient framework integration:

Feel free to create an integration with your favourite framework, let us know so we list it here.

Usage

The required parameters for all hits are Protocol Version, Tracking ID and at least one of these: Client ID or User ID. Some optional ones like IP Override are recommended if you don't want all hits to seem like coming from your servers.

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();

The hit should have arrived to the GA property UA-26293728-11. You can verify this in your Real Time dashboard. Take notice, if you need GA reports to tie this event with previous user actions you must get and set the ClientId to be same as the GA Cookie. Read (here).

The library is 100% done, full documentation is a work in progress, but basically all parameters can be set the same way.

// 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>();

All methods for setting parameters should Autocomplete if you use an IDE such as PHPStorm, which makes building the Analytics object very easy.

Use Cases

Order Tracking with simple E-commerce

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 required hit data
$analytics->setProtocolVersion('1')
    ->setTrackingId('UA-26293624-12')
    ->setClientId('2133506694.1448249699');

// To report an order we need to make single hit of type 'transaction' and a hit of
// type 'item' for every item purchased. Just like analytics.js would do when
// tracking e-commerce from JavaScript
$analytics->setTransactionId(1667) // transaction id. required
    ->setRevenue(65.00)
    ->setShipping(5.00)
    ->setTax(10.83)
    // make the 'transaction' hit
    ->sendTransaction();

foreach ($cartProducts as $cartProduct) {
    $response = $analytics->setTransactionId(1667) // transaction id. required, same value as above
        ->setItemName($cartProduct->name) // required
        ->setItemCode($cartProduct->code) // SKU or id
        ->setItemCategory($cartProduct->category) // item variation: category, size, color etc.
        ->setItemPrice($cartProduct->price)
        ->setItemQuantity($cartProduct->qty)
        // make the 'item' hit
        ->sendItem();
}

Order Tracking with Enhanced E-commerce

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 required hit data
$analytics->setProtocolVersion('1')
    ->setTrackingId('UA-26293624-12')
    ->setClientId('2133506694.1448249699')
    ->setUserId('123');

// Then, include the transaction data
$analytics->setTransactionId('7778922')
    ->setAffiliation('THE ICONIC')
    ->setRevenue(250.0)
    ->setTax(25.0)
    ->setShipping(15.0)
    ->setCouponCode('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 include as many products as you need this way
$productData2 = [
    'sku' => 'AAAA-5555',
    'name' => 'Test Product',
    'brand' => 'Test Brand',
    'category' => 'Test Category 1/Test Category 2',
    'variant' => 'blue',
    'price' => 85.00,
    'quantity' => 2,
    'coupon_code' => 'TEST',
    'position' => 4
];

$analytics->addProduct($productData2);

// Don't forget to set the product action, in this case to PURCHASE
$analytics->setProductActionToPurchase();

// Finally, you must send a hit, in this case we send an Event
$analytics->setEventCategory('Checkout')
    ->setEventAction('Purchase')
    ->sendEvent();

Batch Hits

GA has an endpoint that you can hit to register multiple hits at once, with a limit of 20 hits. Hits to be send can be placed in a queue as you build up the Analytics object.

Here's an example that sends two hits, and then empties the queue.

$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

The queue is emptied when the hits are sent, but it can also be empty manually with emptyQueue method.

$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

If more than 20 hits are attempted to be enqueue, the library will throw a EnqueueUrlsOverflowException.

Validating Hits

From Google Developer Guide:

The Google Analytics Measurement Protocol does not return HTTP error codes, even if a Measurement Protocol hit is malformed or missing required parameters. To ensure that your hits are correctly formatted and contain all required parameters, you can test them against the validation server before deploying them to production.

To send a validation hit, turn on debug mode like this

// 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);

GA actually returns a JSON that is parsed into an associative array. Read (here) to understand how to interpret response.

Disable library hits for Staging/Dev environments

In your app configuration, you can have a flag for enabling or disabling the library, this in order to not actually send hits to GA, in this case the lib returns a AnalyticsResponseInterface object that returns empty values.

This is specially useful in dev or staging environments.

// Instantiate the Analytics object by passing the second parameter in the constructor as TRUE
$analytics = new Analytics(true, true);

Contributors

License

THE ICONIC Google Analytics Measurement Protocol library for PHP is released under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].