-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from zumba/add-troubleshooting
Add way to troubleshoot connection problems when tracking an event
- Loading branch information
Showing
3 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,89 @@ $amplitude->init('APIKEY', '[email protected]') | |
// This is a simple example to get you started, see the rest of the readme for more examples | ||
``` | ||
|
||
## 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 | ||
|
||
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. | ||
|
||
```php | ||
<?php | ||
// Stand-alone Amplitude troubleshooting script - just change APIKEY in next line | ||
$apikey = 'APIKEY'; | ||
|
||
// Composer Autoloader - If new to composer, see https://getcomposer.org | ||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
// Make sure if there is some error, we will see it | ||
ini_set('display_errors', true); | ||
error_reporting(E_ALL); | ||
|
||
// Quick logger to display log messages - NOT for production use, this 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>'; | ||
} | ||
} | ||
} | ||
|
||
$chatty = new ChattyLogger(); | ||
// Test logging an event | ||
?> | ||
<style> | ||
.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; | ||
} | ||
</style> | ||
<h1>Testing Amplitude Log Event Response</h1> | ||
<h2>API Key: '<?= $apikey ?>'</h2> | ||
<?php | ||
$amplitude = new \Zumba\Amplitude\Amplitude(); | ||
|
||
// Add the chatty logger so we can see log messages | ||
$amplitude->setLogger($chatty); | ||
|
||
// Initialize Amplitude with the API key and a dummy test user ID | ||
$amplitude->init($apikey, 'TEST-USER-ID'); | ||
|
||
$chatty->info('Calling $amplitude->logEvent(\'TEST EVENT\')...'); | ||
|
||
// Log a test event | ||
$amplitude->logEvent('TEST EVENT'); | ||
|
||
$chatty->info('Done logging event'); | ||
``` | ||
|
||
### Troubleshooting Tips | ||
|
||
* The Amplitude library will throw a `LogicException` for any problems caused by errors in the code, for instance if you try to log an event without setting the API key first, or try to log an event without specifying the event type. Make sure your server's error logging is set up to display (or otherwise log) any exceptions that might be thrown so that you can see if there is a coding error causing a problem. | ||
* Make sure PHP error logging is enabled (or display errors is enabled), so you can see any PHP errors that may point to the problem. | ||
* Use the `setLogger(...)` method in amplitude to use your app's logging or your own custom logger like the standalone test script above. As long as it implements the `Psr\Log\LoggerInterface`. | ||
* If no logs are generated: It did not attempt to send an event after the point your app's logger was set, or the event was logged using a different instance that does not have a logger set. | ||
* If you see `Curl error:` logged: then something went wrong when it tried to send the request, the error message and context should help point to the problem. | ||
* If there are no curl errors, it will log a message starting with `Amplitude HTTP API response:`: | ||
* `success` with `httpCode = 200` : Amplitude got the request and the event should have been logged. If you are not seeing it in Amplitude, check again after a few minutes, sometimes Amplitude can lag a little behind. | ||
* Anything Else: The event was not logged successfully, refer to the message and context to help troubleshoot the problem. | ||
|
||
# Logging Anonymous Users | ||
|
||
Since this is a PHP SDK, there are a lot of options for tracking Anonymous users. Since this could be run in CLI mode or as a cron job, this SDK does not handle sessions for you. | ||
|
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
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