Snowplow PHP Tracker 0.1.0 released

30 September 2014  •  Joshua Beemster

We are pleased to announce the release of the first version of the Snowplow PHP Tracker. The tracker supports synchronous GET and POST requests.

This introductory post will cover the following topics:

  1. Installation
  2. How to use the tracker
  3. Getting help

The Snowplow PHP Tracker is published to Packagist, the central repository for Composer PHP packages. To add it to your project, add it as a requirement in your composer.json file:

{
    "require": {
        "snowplow/snowplow-tracker": "dev-master"
    }
}

Then simply run composer update from the root of your project to install it.

Add class aliases to the Snowplow Tracker to include it in your project:

use Snowplow\Tracker\Tracker;
use Snowplow\Tracker\Emitter;
use Snowplow\Tracker\Subject;

Create an emitter which will synchronously send HTTP POST requests:

$emitter = new Emitter("d3rkrsqld9gmqf.cloudfront.net");

It is also possible to specify the protocol, method, and port that the emitter will use, as well as a $buffer_size which determines the minimum number of events to queue before sending them all.

Create a subject to hold data about a specific user:

$subject = new Subject();
$subject->setUserId("user-567296");
$subject->setTimezone("Europe/London");
$subject->setLanguage("en");

Create a tracker:

$tracker = new Tracker($emitter, $subject, "my-tracker-namespace", "my-application-id");

Send some events:

// Track a page view
$tracker->trackPageView("http://www.example.com", "title page");

// Track a structured event representing an add-to-basket
$tracker->trackStructEvent("shop", "add-to-basket", null, "red hat", 2);

// Track an ecommerce transaction
$items = array(
    array(
        "sku" => "pbz0026",
        "price" => 20,
        "quantity" => 1,
        "name" => NULL,
        "category" => NULL
    ),
    array(
        "sku" => "pbz0038",
        "price" => 15,
        "quantity" => 1,
        "name" => "shirt",
        "category" => "clothing"
    )
);
$tracker->trackEcommerceTransaction("6a8078be", 35, "USD", "affiliation", 3,
                                    0, "Phoenix", "Arizona", "US", $items);

// Track a Snowplow custom unstructured event
$event_json =
array(
    "schema" => "iglu:com.acme/test/jsonschema/1-0-0",
    "data" => array(
        "page" => "testpage",
        "user" => "tester"
    )
);
$tracker->trackUnstructEvent($event_json);

// Track a screen view event with custom context attached
$custom_context = array(
    "schema" => "iglu:com.snowplowanalytics.snowplow/screen_type/jsonschema/1-0-0",
    "data"=> array(
        "type" => "test",
        "public" => false
    )
);

$user_context = array(
    "schema" => "iglu:com.snowplowanalytics.snowplow/user/jsonschema/1-0-0",
    "data" => array(
        "age" => 40,
        "name" => "Ned"
    )
);

$contexts = array($custom_context, $user_context);

$tracker->trackScreenView("Test screen", "id-0004346", $contexts);

Some useful resources:

This is only the first version of the Snowplow PHP Tracker, so please raise an issue if you find any bugs. If you have an idea for a new feature or need help getting set up, get in touch!