PHP Library


Our PHP Library has the basic functionality laid out in our API specifications. It allows you to record events and set properties. However, you might miss these features from our JavaScript library:

  • No built-in mechanisms for generating and saving identities for your users
  • No built-in mechanisms for automatically tying together anonymous and named identities.
  • No built-in mechanisms for running A/B tests
  • No automatic triggering of Events (such as detecting Search Engine traffic)

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.

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:

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

Example Calls

<?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
?>
Is anything on this page unclear? Suggest edits on Github!