Log an Event

Logging an event is how you start collecting data.

An event can be anything you wish, from tracking page hits and app logins through to form submissions and app usage.

By default, a timestamp of the log will be automatically inserted, and so there is no need to include a timestamp yourself. You can, however, add as much extra information to the log as you wish. This extra information can also be queried on, as we will show later.

 

Code Snippet Example:

 

Log an Event
//to import the analytics classes
$t->runInteraction("Analytics.class.php");

//import config class which contains keys
$t->runInteraction("Config.class.php");

//create object
$analytics = new Event(Config::$ANALYTICS_COLLECTOR);
 
//Log one event
$events = array(
   "name" => "Login",
   "tags" => array(
       "username" => "johnsmith", "group" => "Inspector"
   ),
       "date" => date("c")
);


//Log more then one event
$events = array(
   array(
       "name" => "Login",
       "tags" => array(
           "username" => "johnsmith", "group" => "Inspector"
       ),
       "date" => date("c")
   ),
   array(
       "name" => "Login",
       "tags" => array(
           "username" => "janesmith", "group" => "Inspector"
       ),
       "date" => date("c")
   )
);


//register login events, returns saved event with unique ids for each event
$response = $analytics->log($events);

 

Explanation:

The first two "runInteraction" lines will simply "include" the class files you already uploaded. Make sure your have used the correct interaction names. You will need to put both of these lines on any page, or interaction, that you will be calling the class.

 

//create object
$analytics = new Event(Config::$ANALYTICS_COLLECTOR);

This line will simply create the $analytics object, using the details from the the Config.class.php file. You don't need to modify this line.


//register login events, returns saved event with unique ids for each event
$response = $analytics->log($events
);

This line is the method that will log an event. In this example, you may want to call this after a user successfully logs in.

The $analytics->log method accepts an array(), there are three keys that can be included for each event:

  1. The name of the event using  the "name" key. (Required)
  2. Extra data that you wish to include ( formatted as an array() ) under the "tags" key. (Required)
  3. A date timestamp using the "date" key. (Optional - if left blank, the timestamp will be taken when the data is submitted to the database)

$name (string value)

In the code example above, the event is named "Login". This is can be any string, and you can name it whatever you wish.

This will allow you to group specific events together, and so later you could run an query to show all "Logins", or all "Form Submissions", for example.

$tags (array)

The tags array can contain any extra information you want to include in this log. These are entirely freeform - you can include as much (within reason) or as little information in this array as you wish.

For the sake of performance, please don't include large amounts of data here, such as full form submission data, or images!

In the example above, we're including the username of the person logged in, and the group they are a part of. To add extra information, just add a new value to this array.

Response:

After running this method, you will receive a response object that will simply return the data you logged. You can use this to double check values if you wish, but this is optional.


Next Step:

Create a Query