Collecting data with Trackers and Webhooks

  1. Home
  2. Docs
  3. Collecting data with Trackers and Webhooks
  4. Trackers – collecting data from your own applications
  5. PHP tracker
  6. Initialization

Initialization

To instantiate a new Tracker instance we need to make sure the Snowplow Tracker classes are available.

Include these class aliases in your project:

use Snowplow\Tracker\Tracker; use Snowplow\Tracker\Subject; use Snowplow\Tracker\Emitters\SyncEmitter;

We can now create our Emitter, Subject and Tracker objects.

Creating a Tracker

The most basic Tracker instance will only require you to provide the type of Emitter and the URI of the collector to which the Tracker will log events.

$emitter = new SyncEmitter($collector_uri, "http", "POST", 10, false); $subject = new Subject(); $tracker = new Tracker($emitter, $subject);

Other Tracker arguments:

Argument NameDescriptionRequired?Default
emittersThe emitter to which events are sentYesNone
subjectThe user being trackedYesSubject()
namespaceThe name of the tracker instanceNoNone
app_idThe application IDNoNone
encode_base64Whether to enable base 64 encodingNoTrue

Another example using all of the allowed arguments:

$tracker = new Tracker($emitter, $subject, "cf", "cf29ea", true);

emitters

This can be either a single emitter or an array of emitters. The tracker will send events to all of these emitters, which will, in turn, send them on to a collector.

$emitter1 = new SyncEmitter($collector_uri); $emitter2 = new CurlEmitter($collector_uri, false, "GET", 2); $emitter_array = array($emitter1, $emitter2); // Tracker Init $subject = new Subject(); $tracker1 = ($emitter1, $subject); # Single Emitter $tracker2 = ($emitter_array, $subject); # Array of Emitters

subject

The user which the Tracker will track. This will give your events user-specific data such as timezone and language. You change the subject of your tracker at any time by calling updateSubject($new_subject_object). All events sent from this Tracker will now have the new subject information attached.

namespace

If provided, the namespace argument will be attached to every event fired by the new tracker. This allows you to later identify which tracker fired which event if you have multiple trackers running.

app_id

The app_id argument lets you set the application ID to any string.

encode_base64

By default, unstructured events and custom contexts are encoded into Base64 to ensure that no data is lost or corrupted. You can turn encoding on or off using the encode_base64 argument.