Skip to content

Instantly share code, notes, and snippets.

@evansolomon
Created December 3, 2011 23:11

Revisions

  1. evansolomon revised this gist Feb 1, 2012. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions kissmetrics.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,12 @@
    <?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' );
  2. evansolomon revised this gist Jan 31, 2012. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion kissmetrics.php
    Original file line number Diff line number Diff line change
    @@ -24,9 +24,16 @@ static function init( $key, $time = null ) {
    /**
    * 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;
    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 );
    }
    }

    /**
    @@ -73,6 +80,10 @@ static protected function reset() {
    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;
  3. evansolomon revised this gist Jan 28, 2012. 1 changed file with 12 additions and 5 deletions.
    17 changes: 12 additions & 5 deletions kissmetrics.php
    Original file line number Diff line number Diff line change
    @@ -6,15 +6,19 @@
    define( 'KISSMETRICS_API_KEY', /* Your api key here */ );

    class WP_Kissmetrics {
    static $id = null;
    static $key = null;
    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 ) {
    self::$key = $key;
    static function init( $key, $time = null ) {
    self::$key = $key;
    self::$time = ( ! is_null( $time ) ) ? $time : time();
    }

    /**
    @@ -78,7 +82,10 @@ static protected function is_initialized_and_identified() {
    static protected function generate_query( $type, $data, $update = true ) {

    $data['_k'] = self::$key;
    $data['_t'] = time();
    $data['_t'] = self::$time;

    //Force Kissmetrics to use the time value we pass
    $data['_d'] = 1;

    if( $update )
    $data['_p'] = self::$id;
  4. evansolomon revised this gist Dec 4, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion kissmetrics.php
    Original file line number Diff line number Diff line change
    @@ -94,7 +94,7 @@ static protected function generate_query( $type, $data, $update = true ) {
    /* Actually send Kissmetrics some data */
    static protected function send_query( $query ) {
    $request_url = KISSMETRICS_HOST . $query;
    wp_remote_get( $request_url, array( 'timeout'=>1 ) );
    wp_remote_get( $request_url, array( 'blocking'=>false ) );
    }
    }

  5. evansolomon created this gist Dec 3, 2011.
    106 changes: 106 additions & 0 deletions kissmetrics.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    <?php

    /* 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;

    /**
    * Takes one argument, API key
    * This is required before doing anything else
    */
    static function init( $key ) {
    self::$key = $key;
    }

    /**
    * Identify the user we're going to record data about
    * This is required before recording any data
    */
    static function identify( $id ) {
    self::$id = $id;
    }

    /**
    * 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;
    }

    /* 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'] = time();

    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( 'timeout'=>1 ) );
    }
    }

    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 );