Skip to content

Instantly share code, notes, and snippets.

@EtienneDepaulis
Last active December 13, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EtienneDepaulis/4986872 to your computer and use it in GitHub Desktop.
Save EtienneDepaulis/4986872 to your computer and use it in GitHub Desktop.
Adding Mixpanel tracking for PHP
<?php
class MetricsTracker {
public $token;
public $host = 'http://api.mixpanel.com/';
public function __construct($token_string) {
$this->token = $token_string;
}
function track($event, $properties=array()) {
$params = array(
'event' => $event,
'properties' => $properties
);
if (!isset($params['properties']['token'])){
$params['properties']['token'] = $this->token;
}
$url = $this->host . 'track/?data=' . base64_encode(json_encode($params));
exec("curl '" . $url . "' >/dev/null 2>&1 &");
}
function set($distinct_id, $properties=array(), $ip = "") {
$params = array(
'$distinct_id' => $distinct_id,
'$set' => $properties,
'$token' => $this->token
);
if ($ip != "") {
$params['$ip'] = $ip;
}
$url = $this->host . 'engage/?data=' . base64_encode(json_encode($params));
exec("curl '" . $url . "' >/dev/null 2>&1 &");
}
function increment($distinct_id, $properties=array()) {
$params = array(
'$distinct_id' => $distinct_id,
'$add' => $properties,
'$token' => $this->token
);
$url = $this->host . 'engage/?data=' . base64_encode(json_encode($params));
exec("curl '" . $url . "' >/dev/null 2>&1 &");
}
function track_charge($distinct_id, $amount) {
$params = array(
'$distinct_id' => $distinct_id,
'$append' => array('$transactions' => array('$amount' => $amount, '$time' => substr(date("c"), 0, 19))),
'$token' => $this->token
);
$url = $this->host . 'engage/?data=' . base64_encode(json_encode($params));
exec("curl '" . $url . "' >/dev/null 2>&1 &");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment