Skip to content

Instantly share code, notes, and snippets.

@evansolomon
Created December 3, 2011 23:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evansolomon/1428452 to your computer and use it in GitHub Desktop.
Save evansolomon/1428452 to your computer and use it in GitHub Desktop.
Kissmetrics plugin for WordPress with a simplified class based on their default library
<?php
/*
Plugin Name: WP Kissmetrics
Description: Record events using Kissmetrics' PHP API. Intended to be used by developers only.
Author: evansolomon
Version: 1.0
*/
/* Forked from https://github.com/kissmetrics/KISSmetrics/blob/master/km.php */
define( 'KISSMETRICS_HOST', 'http://trk.kissmetrics.com:80' );
define( 'KISSMETRICS_API_KEY', /* Your api key here */ );
class WP_Kissmetrics {
static $id = null;
static $key = null;
static $time = null;
/**
* Takes one argument, API key
* This is required before doing anything else
* Example: WP_Kissmetrics::init( KISSMETRICS_API_KEY );
* Example: WP_Kissmetrics::init( KISSMETRICS_API_KEY, 1049177863 );
*/
static function init( $key, $time = null ) {
self::$key = $key;
self::$time = ( ! is_null( $time ) ) ? $time : time();
}
/**
* Identify the user we're going to record data about
* This is required before recording any data
* If the user has a Kissmetrics auto-generated ID stored in a cookie, it will be automatically aliased
*/
static function identify( $id ) {
self::$id = $id;
/* Use the Kissmetrics auto-generated ID cookie to alias our logged in ID, then delete the cookie */
if( isset( $_COOKIE['km_ai'] ) ) {
self::alias( $_COOKIE['km_ai'], self::$id );
setcookie( 'km_ai', '', time() - 3600 );
}
}
/**
* Record data about an identified user
* Accepts a single argument 'action' as a string
* Example: WP_Kissmetrics::record( 'Viewed Homepage' );
*
* Also accepts an action + properties of that action as an array
* Example: WP_Kissmetrics::record( 'Purchased upgrade', array( 'product'=>'videopress', 'revenue'=>60');
*/
static function record( $action, $props = array() ) {
if( !self::is_initialized_and_identified() )
return;
$array = array_merge( $props, array(
'_n' => $action,
) );
self::generate_query( 'e', $array );
}
/**
* Alias a new user identiy to a new one in Kissmetrics' data
* Possible use case would be assigning logged out user an identifier and aliasing their User ID after signup
*
* Example: WP_Kissmetrics::alias( get_current_user_id(), $_COOKIE['tmp_km_thing'] );
*/
static function alias( $name, $alias_to ) {
if ( !self::is_initialized() )
return;
$array = array(
'_p' => $name,
'_n' => $alias_to,
);
self::generate_query( 'a', $array, false );
}
/* Protected */
static protected function reset() {
self::$id = null;
self::$key = null;
}
static protected function is_initialized() {
return self::$key;
}
/* Check to make sure both the API key and User ID are set */
static protected function is_initialized_and_identified() {
return self::$key && self::$id;
}
/* Put together the query we're going to send to Kissmetrics */
static protected function generate_query( $type, $data, $update = true ) {
$data['_k'] = self::$key;
$data['_t'] = self::$time;
//Force Kissmetrics to use the time value we pass
$data['_d'] = 1;
if( $update )
$data['_p'] = self::$id;
$query = '/' . $type . '?' . http_build_query( $data, '', '&' );
//Remove some remnants of http_build_query() that Kissmetrics doesn't like
$query = str_replace( '+', '%20', $query );
self::send_query( $query );
}
/* Actually send Kissmetrics some data */
static protected function send_query( $query ) {
$request_url = KISSMETRICS_HOST . $query;
wp_remote_get( $request_url, array( 'blocking'=>false ) );
}
}
function sample_kissmetrics_usage( $product_id ) {
WP_Kissmetrics::init( KISSMETRICS_API_KEY );
WP_Kissmetrics::identify( get_current_blog_id() );
WP_Kissmetrics::record( 'Viewed shopping cart', array( 'user' => get_current_user_id(), 'product' => $product_id ) );
}
add_action( 'view_shopping_cart', 'sample_kissmetrics_usage', 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment