Create a Query
To create reports out of your logged data, you must first create a query.
This will allow you to create common queries that can be reused in multiple projects.
Once you create a Query, when you want to run a report, you will simply call that query. We will cover the reporting on the next page.
Creating a query:
//Include the analytics and configuration classes $t->runInteraction("Analytics.class.php"); $t->runInteraction("Config.class.php"); //Create our query object $query = new Query(Config::$ANALYTICS_ANALYST); $name = "List all #event#"; $type = "find"; $params = array( "name" => "#event#", "date" => array ( "\$gte"=> "#gte#", "\$lte"=> "#lte#" ), "tags.group" => "#group#" ); //save query, returns object with unique Query ID $response = $query->save($name, $type, $params);
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_ANALYST);
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.
//save query, returns object with unique Query ID
$response = $query->save($name, $type, $params);
This line will create your new Query, saving it in your Analytics account for later use.
$name (string)
This is the name of your query - it is only used in the Query Read function (which lists all of your currently saved Queries). This can be any string.
$type (string: find|aggregate)
This is the type of query you are doing. It's value can be either "find" or "aggregate".
- Find: Find will find all matching values
- Aggregate: Aggregate can be used to combine results together, such as reporting the total value of loan forms submitted over a given period, for example.
$params (array)
This array will contain your actual query. The array key you use will be the item you're searching for, and the array value for that key will be what the query will match against.
Notes:
- This array is a standard MongoDB Query, and you can use any MongoDB Query to create highly specific queries, if you wish. MongoDB documentation: https://docs.mongodb.org/manual/reference/operator/query/
- The values wrapped in hash tags, such as #event#, are placeholders, and they will allow you to pass variables to this query when you run the query report. We'll cover this more in the Run a Query Report section.
Search for Specific Events:
In our example:
"name" => "#event#"
The "name" field is the name of the event you specified when calling the "log" class. For example, "Login".
Search by Date Range:
To search by date ranges, we use the Mongo gte (greater than or equal) and lte (less than or equal) values. This is an array under the date key.
"date" => array (
"\$gte"=> "#gte#",
"\$lte"=> "#lte#"
)
For a full list of Comparison Query Operators (like gte and lte), you can view the Mongo documentation here:
https://docs.mongodb.org/manual/reference/operator/query-comparison/
Search by Custom Field:
To search by one of the custom fields you've added, use the format "tags." followed by the name of your custom field.
In our original "log" example we set a custom field for username and group. To search on the "group" option, we use:
"tags.group" => "#group#"
The $params value we've now set will now allow us to run a report based on the name of the log type, between specified date ranges, for all users of a specified group custom field.
Return:
This query will return an array containing the ID of this query (which you need to take a note of), and also the values you set.
array ( [type] => find [params] => {"name":"#event#", "date":{"\$gte":"#gte", "\$lte":"#lte"}, "tags.group":"$group"} [_id] => 56de2fr0219b38e071aac27e [date] => 2016-03-08T01:49:20.459Z [name] => List all #event# )
Next Steps: