Snowplow PHP Tracker 0.1.0 released

Share

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 SnowplowTrackerTracker<span class="p">; use SnowplowTrackerEmitter<span class="p">; use SnowplowTrackerSubject<span class="p">;

Create an emitter which will synchronously send HTTP POST requests:

$emitter = new Emitter("d3rkrsqld9gmqf.cloudfront.net"<span class="p">);

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<span class="p">(); $subject->setUserId("user-567296"<span class="p">); $subject->setTimezone("Europe/London"<span class="p">); $subject->setLanguage("en"<span class="p">);

Create a tracker:

$tracker = new Tracker($emitter, $subject, "my-tracker-namespace", "my-application-id"<span class="p">);

Send some events:

// Track a page view $tracker->trackPageView("http://www.example.com", "title page"<span class="p">); // Track a structured event representing an add-to-basket $tracker->trackStructEvent("shop", "add-to-basket", null, "red hat", 2<span class="p">); // 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" ) <span class="p">); $tracker->trackEcommerceTransaction("6a8078be", 35, "USD", "affiliation", 3, 0, "Phoenix", "Arizona", "US", $items<span class="p">); // Track a Snowplow custom unstructured event $event_json = array( "schema" => "iglu:com.acme/test/jsonschema/1-0-0", "data" => array( "page" => "testpage", "user" => "tester" ) <span class="p">); $tracker->trackUnstructEvent($event_json<span class="p">); // 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 ) <span class="p">); $user_context = array( "schema" => "iglu:com.snowplowanalytics.snowplow/user/jsonschema/1-0-0", "data" => array( "age" => 40, "name" => "Ned" ) <span class="p">); $contexts = array($custom_context, $user_context<span class="p">); $tracker->trackScreenView("Test screen", "id-0004346", $contexts<span class="p">);

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!

Share

Related articles