Skip to content

Instantly share code, notes, and snippets.

@claudeschneider
Created March 2, 2017 19:21
Show Gist options
  • Select an option

  • Save claudeschneider/aa52c74b5f7a222f191f2b8f407846cd to your computer and use it in GitHub Desktop.

Select an option

Save claudeschneider/aa52c74b5f7a222f191f2b8f407846cd to your computer and use it in GitHub Desktop.
PHP function to track events in Amplitude, using their HTTP API
/*
* Usage 1 - track an event
* $arr_event = array("user_id" => $row['user_id'], "event_type" => "Login");
* $arr_event['event_properties'] = array("login_method" => $arg['login_method'], "network" => $arg['network']);
* track_event($arr_event);
*
* Usage 2 - increment a user property
* $arr_event = array("user_id" => $row['user_id'], "event_type" => "increment");
* $arr_event['user_properties'] = array("count_logins" => 1);
* track_event($arr_event);
*/
function track_event($arr_event)
{
global $arr_config;
// https://amplitude.com/docs/api/http
// $arr_event = array()
// "user_id" => 71
// "event_type" => "View Profile" / "increment"
// "event_properties" => array("prop_key" => "prop_value")
// "user_properties" => array("prop_key" => "prop_value")
if(!isset($arr_event['user_id'])) // PHP event tracking requires a user_id to log.
{
return;
}
// always send day and hour (so we can segment on day of week, and time of day) - NB: these should (hopefully) be in the user's local timezone
$arr_event['event_properties']['day'] = date("D");
$arr_event['event_properties']['hour'] = date("H");
// add any UTM params that may be in the URL
if(isset($_REQUEST['utm_medium']) && isset($_REQUEST['utm_source']) && isset($_REQUEST['utm_campaign']))
{
$arr_event['event_properties']['utm_medium'] = $_REQUEST['utm_medium'];
$arr_event['event_properties']['utm_source'] = $_REQUEST['utm_source'];
$arr_event['event_properties']['utm_campaign'] = $_REQUEST['utm_campaign'];
}
if($arr_event['event_type'] == "increment") // if we're incrementing user_properties
{
unset($arr_event['event_type']); // don't need to send this to Amplitude
unset($arr_event['event_properties']); // don't need to send this to Amplitude for identify
$arr_event['user_properties'] = array('$'.'add' => $arr_event['user_properties']); // nest the values within an $add array key
$command = "curl -sS --data 'api_key=".$arr_config['amplitude_api_key']."' --data 'identification=[".json_encode($arr_event)."]' https://api.amplitude.com/identify";
exec($command." > /dev/null &");
}
else // track normal event
{
// add some generic info
if(!isset($arr_event['time'])) // allow time to be set manually if desired
{
$arr_event['time'] = date("U")."000";
}
$arr_event['ip'] = $_SERVER['REMOTE_ADDR'];
$command = "curl -sS --data 'api_key=".$arr_config['amplitude_api_key']."' --data-urlencode 'event=[".json_encode($arr_event)."]' https://api.amplitude.com/httpapi";
exec($command." > /dev/null &");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment