PHP

While it is possible to record data exclusively with the PHP library it is generally used as a supplement to the Javascript library to track purchase data or anything else you might want to track server side. Our PHP Library has the basic functionality laid out in our API specifications. It allows you to record events and set properties. However, the PHP library will be missing these core automatic features from our JavaScript library:

  • Built-in mechanisms for generating and saving identities for your users

  • Built-in mechanisms for automatically tying together anonymous and named identities.

  • Built-in mechanisms for running A/B tests

  • Automatic triggering of Events (such as detecting Search Engine traffic)

  • Using the Event Library and Click to Track For these reasons we recommend our JavaScript Library to our users, and to use the PHP Library for recording events that occur server-side (account Upgrades may be an example of one such event). You might also consider looking at other APIs our customers have created. If you plan to track exclusively with PHP please contact support for more information on fully utilizing your implementation.

Setup

You can download a copy of the API from:

https://github.com/kissmetrics/kissmetrics-php

You will need your API key which you can find in your site settings.

Usage

Before calling any of the common methods, you must initialize with a valid API key:

PHP

<?
  require( 'KISSMetrics/KM.php' );
  $km = new KISSmetrics\Client($KM_KEY, KISSmetrics\Transport\Sockets::initDefault()); // Initialize
?>

Example Calls

PHP

<?php
  $km->identify('bob@example.com')   // Identify user (always)
    ->set(array('gender' => 'male')) // Set a property
    ->record('Viewed Homepage')      // Record an event
    ->record('Signed Up', array('Plan' => 'Pro', 'Amount' => 99.95));     // Record an event with properties
  if (isset( $_COOKIE['km_ai'] )) {
    $km->alias( $_COOKIE['km_ai'] );  // Alias to previously anonymous user, if applicable
  }
  $km->submit();  // Submit all queued items in one method
?>

Last updated