Avo can code generate Avo Functions in PHP for your server side tracking.
Avo functions usage consists of 4 steps.
To get the Avo generated PHP file you must be a member of an Avo workspace with a PHP source. Ask for an invite from a colleague or create a new workspace
TerminalCopy$$$npm install -g avoavo loginavo pull --branch my-branch-name
Learn more about the CLI here.
You can also download the file manually from your Avo workspace.
Import Avo from the generated file and initialize it by calling the init_avo
method before tracking
PHPCopy123require_once 'avo.php';Avo::init_avo(array("env" => "dev" /*, other parameters depending on your tracking plan setup*/), [$system_properties]);
The actual parameters depend on your tracking plan setup, see the parameters explanation in the reference below.
Every event in your tracking plan, marked with the "Implement with Avo" checkbox, gets a function in the generated code, named according to the event name, in snake_case.
For example, if you have a "Signup Start" event defined like this in Avo:
You'll be able to call it like this from the generated code
PHPCopy1Avo::signup_start(array('referral' => 'direct'));
Notice, that you are not passing the System property with the call. System properties are defined on the init step and then automatically included with all events. You can update the system properties with
set_system_properties
function.
Use the Implementation status in your Avo workspace to verify that your implementation is correct.
PHPCopy1public static function init_avo($options, $systemProperties)
Initializes Avo, needs to be called before the tracking methods.
This method will call the make(env, apiKey)
callback in all the provided destination interfaces. It will also initialize the analytics SDKs of the legacy Avo Managed destinations.
options (array)
: an array with env (one of: 'dev', 'prod'), strict bool, verbose bool, logger object, customDestinationInstance object
env
: string, one of 'dev' or 'prod'.[strict = TRUE]
: bool, if set, Avo will throw an exception when it detects a tracking problem in development or staging. Note that the strict flag is ignored in production.[verbose = TRUE]
: bool, if set and strict is false, will print the error messages to console.[logger]
: optional custom implementation of the logger, following the \Psr\Log\LoggerInterface
format. Can be used to disable logs.Custom logger interface:
PHPCopy123456789101112interface LoggerInterface{public function emergency($message, array $context = array());public function alert($message, array $context = array());public function critical($message, array $context = array());public function error($message, array $context = array());public function warning($message, array $context = array());public function notice($message, array $context = array());public function info($message, array $context = array());public function debug($message, array $context = array());public function log($level, $message, array $context = array());}
[destination_instance]
: object, each destination you are sending events to gets a separate parameter in the init function with callbacks that the Avo generated code will trigger, unless you are using the legacy Avo managed destinations. Each method in the destination interface is directly mapped to the Actions attached to each event in Avo. Learn more about event Actions in this doc.PHPCopy12345678910111213141516171819202122232425# Example: Destination interface for the Mixpanel SDK. Replace the Mixpanel implementation with your own tracking SDK methodsclass CustomDestination {private $mp;# This method is optional, you can skip it if you've already initialized your Analytics SDKfunction make($stub, $apiKey){$this->mp = Mixpanel::getInstance(apiKey);}function log_event($user_id, $name, $properties){$properties["distinct_id"] = $user_id;$this->mp->track($name, $properties);}function set_user_properties($user_id, $array){$this->mp->people->set($user_id, $array, $ip = 0, $ignore_time = true);}function log_page($user_id, $page_name, $event_properties){# Note: Mixpanel does not provide a native method for page or screen tracking, so we send an event instead. Other SDKs may have a dedicated page tracking method.$event_properties["distinct_id"] = $user_id;$event_properties["page_name"] = $page_name;$this->mp->track("Page Viewed", $event_properties);}function revenue($userId, $amount, $event_properties){$this->mp->people->trackCharge($userId, $amount);}}
To add the optional anonymousId
parameter to the callbacks in your workspace reach out to us.
Read more about the destination interface here.
PHPCopy1public static function set_system_properties($system_props)
A method to update system properties after initialization.
system_props (array)
: an array containing the system properties that should be sent with every event.
When you define system properties in your Avo workspace you set name and type - the keys in this dictionary should be the same as system properties, in snake_case, and you should provide corresponding types, can be string, int, long, float, bool, array and object.
PHPCopy1public static function [your_event_name]($properties = [])
Every event you define in your tracking plan in Avo gets a function named after the event in snake_case. The arguments of the function depend on how it's defined in your tracking plan.
properties
: an associated array of properties, defined in your Avo tracking plan. Properties can be of types string, int, long, float, bool, array and object.
Keys are:
user_id_
: string, added to all events, used to connect event to specific userYou can send your data using the Avo generated PHP code to any data destination that accepts custom events, including: