Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Once you have created a query, you can run a report to return the query result.

...

Code Block
languagephp
themeRDark
firstline1
titleCode Block Example
linenumberstrue
//Include the analytics class and the configuration class
$t->runInteraction("Analytics.class.php");
$t->runInteraction("Config.class.php");
 
//Create our query object
$query = new Query(Config::$ANALYTICS_ANALYST);

//check if the query exists
$res = $query->get();

//find the QueryID  
$queryId = $res[0]["_id"];

//You need to run the query on the collector key
$collection = Config::$ANALYTICS_COLLECTOR["ACCESS"];
  	
//build query string to replace placeholders
$queryString =
array(
    "event" => "Login",
    "gte" => "date:2016-03-21",
    "lte" => "date:2030-04-30", 
  	"group" => "Inspector"
);


//authenticate and create a new report, returns report object
$analytics = new Report(Config::$ANALYTICS_ANALYST);

//run the query, returns an array
$response = $analytics->runQuery($queryId, $collection, $queryString); 


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 our query object
$query = new Query(Config::$ANALYTICS_ANALYST);

This line will simply create the $query object, using the analyst credentials from the the Config.class.php file.

 

//Read all saved queries
$res = $query->get();

This line will will read all saved queries (there are no search options available).

 

//find the queryID
$queryId = $res[0]["_id"];

 Each query has a unique id, you need this ID to run the query and return data.

 

//You need to run the query on the collector key
$collection = Config::$ANALYTICS_COLLECTOR["ACCESS"];

When gathering saved data, you need to do so using the Collector key.

 

//authenticate and create a new report, returns report object
$analytics = new Report(Config::$ANALYTICS_ANALYST);

Using the Analyst key, set up a new Report.

 

//run the query, returns an array
$response = $analytics->runQuery($queryId, $collection, $queryString);

Run a query to receive a response containing the requested data.

$queryId (string)

This is the Query ID of the (previously set up) query you wish to use. Your Query ID will have been returned (in the return object) when you first saved the query.

Example:

 

Code Block
languagephp
themeRDark
firstline1
titleCode Block Example
linenumberstrue
/* --- Method 1: Find the QueryID in the return array after saving a query --- */
//save query, returns object with unique Query ID
$res = $query->save($name, $type, $params);

//find the queryID
$queryId = $res["_id"];


/* --- Method 2: query all saved queries and choose the appropriate QueryID --- */
//Alternately, you can use $query->get() to return all saved queries
$res = $query->get();
           
//find the QueryID
$queryId = $res[0]["_id"];

$collection(string)

Your analytics collector access key.

Your collector access key will be a 24 character string and will be the same key the was provided to you by Blink when your Analytics account was set up. See API Key Pairs for more information.

$queryString(array)

This is the query to run. This query should reference all placeholders that were set up when creating the query.

If you need to run a query with different criteria, you will have to create and use a different query (See: Create a Query).

 


Return:

This report will return an array containing the events that have been logged against the query. If you have been following the examples in this documentation your response would look similar to this:

...