Querying Events


Overview

You can query your data from anywhere in your stack by simply making a HTTP request to the following endpoint

GET https://api.blueradar.net/event

This endpoint requires the following:

  • site_id (required)
    • string
  • api_token (required)
    • string
  • event (required)
    • string
    • the name of the event you want to query
  • from (required)
    • string | numeric | DateTime
    • the lower bound of when the events were tracked (UTC)
      1. for example: "2022-07-25 06:23:47"
  • to (required)
    • string | numeric | DateTime
    • the upper bound of when the events were tracked (UTC)
      1. for example: "2022-09-25 06:23:47"
  • aggregate_type (optional)
    • "count" | "sum" | "avg"
    • how the value fields of the rows should be aggregated - default is count
      1. Count - simply count the number of rows of the event
      2. Sum - sum the values together
      3. Avg - return the average of the rows

Response

When successful the api will return a http status of 200 with the following format

[
  {
    "timestamp": "string",
    "count": number
  },
  ...
]

Any other status will include either an error message as text

missing api_token field

or a json object with just null if the query failed to execute


Laravel Example

use Carbon\Carbon;
use Illuminate\Support\Facades\Http;

class BlueRadar
{
  static function get(
    string $event,
    Carbon $from,
    Carbon $to,
    string $aggregate_type = "count"
  ) {
    $siteId = env("BLUERADAR_SITE_ID");
    $apiToken = env("BLUERADAR_SITE_API_TOKEN");

    if (!$siteId || !$apiToken) {
      return null;
    }

    if (!in_array($aggregate_type, ["count", "sum", "avg"])) {
      return null;
    }

    $res = Http::get("https://api.blueradar.net/event", [
      "site_id" => $siteId,
      "api_token" => $apiToken,
      "event" => $event,
      "from" => $from,
      "to" => $to,
    ]);

    if (!$res->successful()) {
      throw new \Exception(
        "Server responded with {$res->status()} for query '{$event}'"
      );
    }

    return $res->json();
  }
}

Usage

use BlueRadar;

class AnalyticsController extends Controller
{
  public function index()
  {
    $data = BlueRadar::get(
      "invoices",
      Carbon::today()->subMonth(),
      Carbon::now(),
      "sum" // get total revenue
    );

    return view("analytics", [
      "data" => $data,
    ]);
  }
}

CURL Example

curl -X GET https://api.blueradar.net/event \
-d site_id="YOUR_SITE_ID" \
-d api_token="YOUR_API_TOKEN" \
-d event="YOUR_EVENT_NAME" \
-d from=FROM_DATE \
-d to=TO_DATE \
-d aggregate_type="AGGREGATE_TYPE"

JS Example

fetch("https://api.blueradar.net/event", {
  body: new URLSearchParams({
    site_id: "YOUR_SITE_ID",
    api_token: "YOUR_API_TOKEN",
    event: "YOUR_EVENT_NAME",
    from: "FROM_DATE",
    to: "TO_DATE",
    aggregate_type: "AGGREGATE_TYPE", // "count" | "sum" | "avg"
  }),
});

Python Example

import requests

data = {
  'site_id': 'YOUR_SITE_ID',
  'api_token': 'YOUR_API_TOKEN',
  'event': 'YOUR_EVENT_NAME',
  'from': 'FROM_DATE',
  'to': 'TO_DATE',
  'aggregate_type': 'AGGREGATE_TYPE', // "count" | "sum" | "avg"
}

response = requests.get('https://api.blueradar.net/event', data=data)

PHP Example

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.blueradar.net/event");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  "Content-Type" => "application/x-www-form-urlencoded",
]);
curl_setopt(
  $ch,
  CURLOPT_POSTFIELDS,
  "site_id=YOUR_SITE_ID&api_token=YOUR_API_TOKEN&event=YOUR_EVENT_NAME&from=FROM_DATE&to=TO_DATE&aggregate_type=AGGREGATE_TYPE"
);

$response = curl_exec($ch);

curl_close($ch);