GithubHelp home page GithubHelp logo

kniepok / php-ga-measurement-protocol Goto Github PK

View Code? Open in Web Editor NEW

This project forked from theiconic/php-ga-measurement-protocol

0.0 2.0 0.0 210 KB

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

License: MIT License

PHP 100.00%

php-ga-measurement-protocol's Introduction

Google Analytics Measurement Protocol library for PHP

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads License Documentation Status Dependency 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

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"
    }
}

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 Client 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.

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

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

Asynchronous Requests (Non-Blocking)

By default, sending a hit to GA will be a synchronous request, and block the execution of the script until it gets a response from the server or timeouts after 100 secs (throwing a Guzzle exception). However, an asynchronous non-blocking request can be sent by calling setAsyncRequest(true) before sending the hit:

// When building the Analytics hit, just make a call to the setAsyncRequest method passing true
// now sending the hit won't block the execution of the script
$analytics
    ->setAsyncRequest(true)
    ->sendPageview();

This means that we are sending the request and not waiting for a response. The AnalyticsResponse object that you will get back has NULL for HTTP status code.

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
// First, general and required hit data
$analytics->setProtocolVersion('1')
    ->setTrackingId('UA-26293624-12')
    ->setClientId('12345678')
    ->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, only required fields are SKU and Name
$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();

Contributors

License

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

php-ga-measurement-protocol's People

Contributors

jorgeborges avatar jfalcondb avatar baibaratsky avatar foaly-nr1 avatar lombo avatar irazasyed avatar

Watchers

James Cloos avatar Krzysztof Niepokojczycki avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.