Skip to content

Commit

Permalink
Merge pull request #12 from MindscapeHQ/user_tracking
Browse files Browse the repository at this point in the history
User tracking
  • Loading branch information
Callum committed Oct 13, 2013
2 parents f01772f + ee605ce commit 2743b49
Show file tree
Hide file tree
Showing 7 changed files with 1,298 additions and 7 deletions.
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -75,6 +75,16 @@ If the handlers reside in their own file, just import it in every file where you

You can transmit the version number of your PHP project along with the message by calling `SetVersion()` on your RaygunClient after it is instantiated - this is optional but recommended as the version number is considered to be first-class data for a message.

#### User tracking

You can call $client->SetUser($user), passing in a string representing the username or email address of the current user of the calling application. This will be attached to the message and visible in the dashboard. This method is optional - if it is not called, a random identifier will be used. If you use this, and the user changes (log in/out), be sure to call it again passing in the new user (or just call $client->SetUser() to assign a new random identifier).

## Troubleshooting

SendError and SendException return the HTTP status code of the transaction - `echo`ing this will give you a 403 if your API key is incorrect or a 202 if everything was a success.
SendError and SendException return the HTTP status code of the transaction - `echo`ing this will give you a 403 if your API key is incorrect or a 200 if everything was a success.

## Changelog

* Version 1.1: Added user tracking support; improved experience in CLI mode; add user-specified timestamp support

* Version 1.0: Initial commit
20 changes: 20 additions & 0 deletions src/Raygun4php/Exception/UnsatisfiedDependencyException.php
@@ -0,0 +1,20 @@
<?php
/**
* This file is part of the Rhumsaa\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/

namespace Rhumsaa\Uuid\Exception;

/**
* Thrown to indicate that the requested operation has depencies that have not
* been satisfied.
*/
class UnsatisfiedDependencyException extends \RuntimeException
{
}
19 changes: 19 additions & 0 deletions src/Raygun4php/Exception/UnsupportedOperationException.php
@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the Rhumsaa\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/

namespace Rhumsaa\Uuid\Exception;

/**
* Thrown to indicate that the requested operation is not supported.
*/
class UnsupportedOperationException extends \RuntimeException
{
}
67 changes: 61 additions & 6 deletions src/Raygun4php/RaygunClient.php
Expand Up @@ -3,17 +3,26 @@
{
require_once realpath(__DIR__.'/RaygunMessage.php');
require_once realpath(__DIR__.'/Raygun4PhpException.php');
require_once realpath(__DIR__.'/Uuid.php');

use Rhumsaa\Uuid\Uuid;

class RaygunClient
{
protected $apiKey;
protected $version;
protected $tags;
protected $user;
protected $httpData;

public function __construct($key)
{
$this->apiKey = $key;

if (session_id() == "")
{
session_start();

This comment has been minimized.

Copy link
@mrardon

mrardon Oct 20, 2013

Contributor

Starting a session like this is not good - it breaks frameworks like Symfony that do sessions there own way. - Got this error: Failed to start the session: already started by PHP

}
}

/*
Expand All @@ -29,6 +38,10 @@ public function __construct($key)
*/
public function SendError($errno, $errstr, $errfile, $errline, $tags = null, $userCustomData = null, $timestamp = null)
{
if ($this->user == null)
{
$this->SetUser();
}
$message = $this->BuildMessage(new \ErrorException($errstr, $errno, 0, $errfile, $errline), $timestamp);

if ($tags != null)
Expand All @@ -38,7 +51,7 @@ public function SendError($errno, $errstr, $errfile, $errline, $tags = null, $us
if ($userCustomData != null)
{
$this->AddUserCustomData($message, $userCustomData);
}
}
return $this->Send($message);
}

Expand All @@ -52,6 +65,10 @@ public function SendError($errno, $errstr, $errfile, $errline, $tags = null, $us
*/
public function SendException($exception, $tags = null, $userCustomData = null, $timestamp = null)
{
if ($this->user == null)
{
$this->SetUser();
}
$message = $this->BuildMessage($exception, $timestamp);

if ($tags != null)
Expand Down Expand Up @@ -79,6 +96,33 @@ public function SetVersion($version)
$this->version = $version;
}

/*
* Stores the current user of the calling application. This will be added to any messages sent
* by this provider. It is used in the dashboard to provide unique user tracking.
* If it is an email address, the user's Gravatar can be displayed. This method is optional,
* if it is not used a random identifier will be assigned to the current user.
* @param string $user A username, email address or other identifier for the current user
* of the calling application.
*
*/
public function SetUser($user = null)
{
if (is_string($user))
{
$this->user = $user;
$_SESSION['rguserid'] = $user;
}
else
{
if (empty($_SESSION['rguserid']))
{
$_SESSION['rguserid'] = (string) Uuid::uuid4();
}

$this->user = $_SESSION['rguserid'];
}
}

/*
* Sets a string array of tags relating to the message,
* used for identification. These will be transmitted along with messages that
Expand All @@ -89,7 +133,18 @@ private function BuildMessage($errorException, $timestamp = null)
{
$message = new RaygunMessage($timestamp);
$message->Build($errorException);
$message->Details->Version = $this->version;
$message->Details->Version = $this->version;
$message->Details->Context = new RaygunIdentifier(session_id());

if ($this->user != null)
{
$message->Details->User = new RaygunIdentifier($this->user);
}
else
{
$message->Details->User = new RaygunIdentifier($_SESSION['rguserid']);
}

return $message;
}

Expand Down Expand Up @@ -139,10 +194,10 @@ public function Send($message)
throw new \Raygun4php\Raygun4PhpException("API not valid, cannot send message to Raygun");
}
else
{
if (!$this->httpData) {
$this->httpData = curl_init('https://api.raygun.io/entries');
}
{
if (!$this->httpData) {
$this->httpData = curl_init('https://api.raygun.io/entries');
}
curl_setopt($this->httpData, CURLOPT_POSTFIELDS, json_encode($message));
curl_setopt($this->httpData, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->httpData, CURLINFO_HEADER_OUT, true);
Expand Down
13 changes: 13 additions & 0 deletions src/Raygun4php/RaygunIdentifier.php
@@ -0,0 +1,13 @@
<?php
namespace Raygun4php
{
class RaygunIdentifier
{
public $Identifier;

public function __construct($id)
{
$this->Identifier = $id;
}
}
}
2 changes: 2 additions & 0 deletions src/Raygun4php/RaygunMessageDetails.php
Expand Up @@ -11,5 +11,7 @@ class RaygunMessageDetails
public $Version;
public $Tags;
public $UserCustomData;
public $User;
public $Context;
}
}

0 comments on commit 2743b49

Please sign in to comment.