Last active
December 13, 2015 22:49
-
-
Save EtienneDepaulis/4986872 to your computer and use it in GitHub Desktop.
Adding Mixpanel tracking for PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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