PHPackages                             zumba/amplitude-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zumba/amplitude-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zumba/amplitude-php
===================

PHP SDK for Amplitude

1.0.9(5mo ago)409.5M—4.3%18[2 issues](https://github.com/zumba/amplitude-php/issues)5MITPHPPHP &gt;=7.2CI passing

Since Apr 29Pushed 5mo ago25 watchersCompare

[ Source](https://github.com/zumba/amplitude-php)[ Packagist](https://packagist.org/packages/zumba/amplitude-php)[ Docs](https://github.com/zumba/amplitude-php)[ RSS](/packages/zumba-amplitude-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (12)Used By (5)

Zumba amplitude-php
===================

[](#zumba-amplitude-php)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://github.com/zumba/amplitude-php/workflows/Amplitude%20PHP%20CI/badge.svg)](https://github.com/zumba/amplitude-php/workflows/Amplitude%20PHP%20CI/badge.svg)[![Coverage Status](https://camo.githubusercontent.com/4925576e7bfd7193b108b145eea1142f56e513a6ed952bddc82d3f70d7e9d41f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7a756d62612f616d706c69747564652d7068702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/zumba/amplitude-php?branch=master)[![Scrutinizer](https://camo.githubusercontent.com/9e924310f0b7d16e1603d627c3de9d9f229a34b0c6b5bd3392dc78a70db66b51/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7a756d62612f616d706c69747564652d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/zumba/amplitude-php/)

This is a moderately thin PHP API for [Amplitude](https://amplitude.com/), powerful enough to do what you need without getting in the way. Designed to work well in 2 main scenarios:

- **Multiple Events using Same User &amp; Amplitude App** - When you are tracking possibly multiple events, all for the same user, all for the same Amplitude app. This library provides a Singleton instance that allows initializing the API key and user info once for the page load, that gets used for any events logged during that page load.
- **Multiple Events across Multiple Users and possibly multiple Amplitude Apps** - For times you may need to log multiple events for a lot of different users, possibly using different amplitude apps.

Example
=======

[](#example)

```
// 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 = \Zumba\Amplitude\Amplitude::getInstance();
$amplitude->init('APIKEY', 'johnny@example.com')
    ->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.  If Amplitude is already initialized, this will send the event
// to Amplitude right away (only uses a queue for early-logged events)
\Zumba\Amplitude\Amplitude::getInstance()
    ->queueEvent('EVENT TYPE');

// Can pass in an array for the second parameter to set event properties
\Zumba\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
```

Getting Started &amp; Troubleshooting
-------------------------------------

[](#getting-started--troubleshooting)

When you are initially getting your application set up, if you do not see your event show up in Amplitude, you may need to do a little troubleshooting. Normally your indication that "it worked" is when you see your event show up in your Amplitude app for the first time.

If you never see that first event show up, you can see what Amplitude's response is when the event is logged. This may help find and fix the problem (such as an invalid API key, PHP environment errors, connection problems, etc.)

Amplitude uses `Psr\Logger` for logging the communication with the Amplitude HTTP API. You can take advantage of this by setting a logger (using `$amlitude->setLogger()`) to help catch any problems.

### Stand-alone Troubleshooting Script

[](#stand-alone-troubleshooting-script)

Below is a stand-alone script, meant to be copied into a PHP file at the root of your application's document root. Just change the `APIKEY` and if needed, adjust the line that requires the `autoload.php` file for composer. Then visit the script's URL from a browser to see any messages logged.

```

.code {
    display: inline-block;
    border: 1px solid #a7a7a7;
    padding: 15px;
    margin: 0 5px;
    background-color: #eaeaea;
    white-space: pre;
}
p {
    padding-bottom: 5px;
    border-bottom: thin dashed gray;
}

Testing Amplitude Log Event Response
API Key: ''
