1. Go to this page and download the library: Download viezel/amplitude-php-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
viezel / amplitude-php-sdk example snippets
// After User is Initialized in your application, set the user info in Amplitude that you want to track (minimally
// the user identifier or device identifier, and of course your Amplitude App API key)
$amplitude = \Viezel\Amplitude\Amplitude::getInstance();
$amplitude->init('APIKEY', '[email protected]')
->setUserProperties([
'dob' => '1980-11-04',
'name' => 'Johnny 5'
])
// Only call this once API Key and user ID or device ID is set
->logQueuedEvents();
// -- Meanwhile, in another part of the code... --
// Anywhere else in your application that needs to log an event
// This will even work if called before the above code initializes Amplitude! If that is case, it will queue it
// and send the event when logQueuedEvents() is called.
\Viezel\Amplitude\Amplitude::getInstance()
->queueEvent('EVENT TYPE');
// Can pass in an array for the second parameter to set event properties
\Viezel\Amplitude\Amplitude::getInstance()
->queueEvent('SECOND EVENT', ['quantity' => 1, 'price' => 15.32, 'Custom Property' => 'Widgets']);
// This is a simple example to get you started, see the rest of the readme for more examples
// After your application has set up the session (for instance in your bootloader or similar), initialize Amplitude:
$amplitude = \Viezel\Amplitude\Amplitude::getInstance();
// Notice we are not setting second parameter here for user ID, we will do that below if it is available
$amplitude->init('APIKEY');
// Can use the PHP session ID, or alternatively, any unique string your application uses to track sessions
$sessionId = session_id();
// Keep track of whether we have a session or user ID
$canLogEvents = false;
if (!empty($sessionId)) {
$amplitude->setDeviceId($sessionId);
$canLogEvents = true;
}
// Presumes $applicationUserId set prior to this by your application
if (!empty($applicationUserId)) {
$amplitude->setUserId($applicationUserId);
$canLogEvents = true;
}
if (!empty($userData)) {
// If you have other user properties, set them as well... They will be set on the first event sent to Amplitude
$amplitude->setUserProperties($userData);
}
if ($canLogEvents) {
// Make sure to send any events that may have gotten queued early
$amplitude->logQueuedEvents();
} else {
// Do not have a user ID or device ID for this page load, so set `optOut`, to prevent amplitude from trying
// to send events (since it won't work without user or device ID)
$amplitude->setOptOut(true);
}
// -- Meanwhile, in another part of the code... --
// Just queue events as normal
\Viezel\Amplitude\Amplitude::getInstance()->queueEvent('EVENT_TYPE');
$event = new \Viezel\Amplitude\Event();
// Method 1 - set user properties method:
$event->setUserProperties(
[
'name' => 'Rambo',
// ...
]
);
// If you called setUserProperties() a second time, it would overwrite any properties with the same name but leave
// others intact
// Method 2 - just set the userProperties directly:
$event->userProperties = [
'name' => 'Mary',
// ...
];
// This works just like you would expect: it will reset what is already there.
// Note that prior to anything being set, $event->userProperties will be null, not an empty array
// Here, we are not using Singleton as we will only use this connection to send these batch user events, we don't
// want any user data from the Singleton instance to accidentally bleed into the first user's event
$amplitude = new \Viezel\Amplitude\Amlitude();
// Alternatively, if we wanted to re-use the same Amplitude object with the same key elsewhere in the code, could
// have used:
// $amplitude = \Viezel\Amplitude\Amplitude::getInstance('NAMED-INSTANCE');
// That will maintain the same Amplitude instance anywhere that requests that specific name.
$amplitude->init('APIKEY');
// $userEvents might be an array your application generates with user info and events that need to be sent
foreach ($userEvents as $myUserEvent) {
$event = $amplitude->event();
// Notice below we are setting user ID and user data on the event itself, not inside Amplitude where it would end
// up persisting the user ID between logged events...
// The below assumes an array set like so:
/*
$myUserEvent = [
'id' => 'user-id',
'user_details' => [], // key/value array of user info
'event_type' => 'EVENT', // event to log
'event_properties' => [], // key/value array of event properties to set
];
*/
$event->userId = $myUserEvent['id'];
$event->userProperties = $myUserEvent['user_details'];
$event->eventType = $myUserEvent['event_type'];
$event->set($myUserEvent['event_properties']);
// Since we used $amplitude->event() to get event object, it will be the event to be sent when we call this
$amplitude->logEvent();
// Above we are using logEvent instead of queueEvent since the code is not "spread out", we can ensure that
// amplitude is already initialized and all the
// Send just event with no event properties:
\Viezel\Amplitude\Amplitude::getInstance()
->logEvent('EVENT-NAME');
// Queue event and add a property:
\Viezel\Amplitude\Amplitude::getInstance()
->logEvent('EVENT-NAME', ['property1' => 'value1']);
// Get the next event that will be queued or sent:
$event = \Viezel\Amplitude\Amplitude::getInstance()->event();
// Set up the event here, by setting properties...
$event->eventType = 'EVENT-NAME';
// Queue or send the event - since we got the event using the event method, it will be the one used on the next
// queue or send, no need to pass it back in.
\Viezel\Amplitude\Amplitude::getInstance()->queueEvent();
// Will queue current stored event. $eventType or $props are
// provided, it will erase the matching props and the event type.
// You can also log your stored event directly.
\Viezel\Amplitude\Amplitude::getInstance()->logEvent();
// First, probably the most common, you can use the magic set methods to just set the property like this:
$event->propertyName = 'property value';
// Set using set(), handy for property names that are invalid as PHP variables:
$event->set('Property name with Space', 'property value');
// Set can be chained:
$event->set('prop1', 'val1')
->set('prop2', 'val2')
->set('prop3', 'val3');
// Pass in array of properties for the first parameter:
$event->set(
[
'prop1' => 'val1',
'prop2' => 'val2',
]
);
// Convenience way to quickly add properties to an event, just pass in array of properties to the event method:
\Viezel\Viezel\Amplitude::getInstance()->event(
[
'eventProp' => 'Event Value',
'productId' => 'acme-widget-45',
'price' => 15.32,
]
);
// The above is equivalent to:
$event = \Viezel\Viezel\Amplitude::getInstance()->event();
$event->setProperties(
[
'eventProp' => 'Event Value',
'productId' => 'acme-widget-45',
'price' => 15.32,
]
);
// For non-standard property names, use the unsetProperty method:
$event->unsetProperty('My Location');
// Magic unset also works
unset($event->productId);
$event = new \Viezel\Amplitude\Event();
// Set event properties here
// Pass the event into amplitude and queue it
\Viezel\Amplitude\Amplitude::getInstance()
->event($event)
->queueEvent();
$event = \Viezel\Amplitude\Amplitude::getInstance()->event();
// Set event properties here
// Send that event
\Viezel\Amplitude\Amplitude::getInstance()->queueEvent();
$event = \Viezel\Amplitude\Amplitude::getInstance()->event();
$event->eventType = 'Complicated Event';
// -- Meanwhile, in another part of the code... --
// As long as the event has not yet been sent or queued up, you can get it and change it as needed:
$event = \Viezel\Amplitude\Amplitude::getInstance()->event();
$event->deviceId = 'DEVICE ID';
// Just remember, once finished setting up the event, call queueEvent() or logEvent() once.
\Viezel\Amplitude\Amplitude::getInstance()->queueEvent();
// Either set it this way:
$event->eventType = 'EVENT';
// OR set it when logging/queuing the event:
\Viezel\Amplitude\Amplitude::getInstance()
->queueEvent('EVENT');
$amplitude = \Viezel\Amplitude\Amplitude::getInstance()
->init('APIKEY');
foreach ($eventFactory->getEvents() as $event) {
$amplitude->event($event)
->queueEvent();
}
// And then send all queued events.
$amplitude->logQueuedEvents();
// Stand-alone Amplitude troubleshooting script - just change APIKEY in next line
$apikey = 'APIKEY';
// Composer Autoloader - If new to composer, see https://getcomposer.org
displays log message to the browser
class ChattyLogger extends \Psr\Log\AbstractLogger
{
public function log($level, $message, array $context = [])
{
echo "<p><strong>".ucfirst($level).":</strong> $message<br>";
if (!empty($context)) {
echo '<strong>Context:</strong><br><span class="code">'.print_r($context,true).'</span>';
}
echo '</p>';
}
}
$chatty = new ChattyLogger();
// Test logging an event
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.