PHP Invocation

padams edited this page Nov 6, 2015 · 1 revision

OWA's PHP tracking client can be used within your PHP application to record page views, e-commerce transaction, action events, and custom events that you create.

Adding The PHP tracker to your PHP

The PHP tracker can added to your application with a few lines of code.

    <?php
        require_once('owa_env.php');
        require_once(OWA_DIR.'owa_php.php');
        $owa = new owa_php();
        $owa->setSiteId('your_site_id');
        $owa->setPageTitle('My Web Page Title');
        $owa->setPageType('article');
        $owa->trackPageView();
    ?>

Let's take a look at this code line by line:

require_once('/path/to/owa_env.php');

This include sets up OWA's environment including defineing paths used in subsequent includes.

require_once(OWA_DIR.'owa_php.php');

This includes the tracking client class.

$owa = new owa_php();

This creates a new tracking client object.

$owa->setSiteId('your_site_id');

This method call sets the ID of the web site that you are tracking. This site ID will be added to all events tracked by the client. Site IDs are created by adding a tracked site to OWA via the site roster page of OWA's web interface.

$owa->setPageTitle('My Web Page Title');

This method sets the page title property of the page being tracked.

$owa->setPageType('article');

This method sets the page type property of the page being tracked.

$owa->trackPageView();

This method tracks the page view event.

Click and DomStream Tracking

You cannot use the PHP tracking client to record clicks and DomStreams. However OWA's PHP tracker has a convenience method called placehelperPageTags that can be used to include the tracker just to track clicks and domstreams.

placeHelperPageTags()

    <?php
        $owa = & new owa_php;
        $owa->placeHelperPageTags();
    ?>

This code with echo tags into your web page so be sure to place it within the BODY of your HTML document where you want them. Alternatively you can tell placePageTags ''not'' to echo the tag code but instead store it into a variable that you can then pass to some other part of your app/script that handles page output in a more orderly fashion:

    <?php
        $owa = & new owa_php;
        $tag_code = $owa->placeHelperPageTags(false);
    ?>

Tracking Custom Events

The PHP Tracker can also be used to track custom event types that you define. The following is sample code for tracking a custom event called "video_play".

    // Create a new Instance of the tracker
    $owa = new owa_php($config);
    // Set the ID of the site being tracked
    $owa->setSiteId('your_site_id');
    // Create a new event object
    $event = $owa->makeEvent();
    // Set the Event Type, in this case a "video_play"
    $event->setEventType('video_play');
    // Set a property
    $event->set('video_id', 54345);
    // Track the event
    $owa->trackEvent($event);

This code will record the event and make it to any event handlers that are listening for it. Because this is a custom event type you will need create a custom event handler and register it to listen for it and act on it (record it to a DB table etc.).