SlicingDice Documentation

SlicingDice API Docs

Welcome to the SlicingDice API documentation. You'll find comprehensive guides and documentation to help you start working with SlicingDice as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    Guides

Count events


A SlicingDice count events query is equivalent to a SQL query like this:

SELECT COUNT(*) 
FROM   UserEvents 
WHERE  UserEvents.Actions = 'Add to Cart' 
       AND UserEvents.Timestamp BETWEEN 
           '{% convert_date "YYYY-MM-DDT00:00:00Z" "-15d" %}' 
           AND 
           '{% convert_date "YYYY-MM-DDT00:00:00Z" "-1d" %}'; 

Query purpose

You should use count events to know the number of times some data was inserted for event columns in a particular dimension.

Count event queries are ideal to answer this kind of questions:

  • How many times the event visited-page for the actions column happened in the last 24 hours?
  • How many times the value for the purchases-values column was greater-than 1000 and happened between {% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %} and {% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}?

Query endpoint

In order to make a count events query, you simply need to send a POST request to https://api.slicingdice.com/v1/query/count/events with an array of JSON object (or a single JSON object) of queries identified by unique names, each containing a list of conditions.

Pro Tip

Remember that you can also use test databases to query before inserting real data to your production database.

Consider generating a free demo database key for yourself to use in all examples below, as indicated on the try before you buy page.


Query syntax and structure

The between predicate is required in the conditions, along with one of the following:

Also, note it is not possible to use boolean operators, such as not, and and or in count events queries.

In this query below we will count how many times the event visited-page for the actions column happened between {% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %} and {% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}.

SELECT COUNT(*) FROM users WHERE [actions.value] = 'visited-page' AND [actions.date] BETWEEN '{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}' AND '{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}';
$ curl -X POST https://api.slicingdice.com/v1/sql \
    -H 'Authorization: MASTER_OR_WRITE_API_KEY' \
    -H 'Content-Type: application/sql' \
    -d 'SELECT COUNT(*) FROM users WHERE [actions.value] = 'visited-page' AND [actions.date] BETWEEN '{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}' AND '{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}';'
curl -X POST https://api.slicingdice.com/v1/query/count/event \
    -H 'Authorization: MASTER_OR_READ_API_KEY' \
    -H 'Content-Type: application/json' \
    -d '[
    {
        "query-name": "visited-page-events",
        "query": [
            {
                "actions": {
                    "equals": "visited-page",
                    "between": [
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}",
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}"
                    ]
                }
            }
        ]
    }
]'
from pyslicer import SlicingDice
slicingdice = SlicingDice(master_key='MASTER_API_KEY')

query_data = [
    {
        "query": [
            {
                "actions": {
                    "equals": "visited-page",
                    "between": [
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}", 
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}"
                    ]
                }
            }
        ],
        "query-name": "visited-page-events"
    }
]

print(slicingdice.count_event(query_data))

import com.slicingdice.jslicer.SlicingDice;
import java.io.IOException;
import org.json.JSONObject;
import org.json.JSONArray;

public class Example {
    public static void main(String[] args) throws IOException {
        SlicingDice slicingdice = new SlicingDice("MASTER_API_KEY");

        JSONArray queryData = new JSONArray()
        .put(new JSONObject()
            .put("query", new JSONArray()
                .put(new JSONObject()
                    .put("actions", new JSONObject()
                        .put("equals", "visited-page")
                        .put("between", new JSONArray()
                            .put("{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}")
                            .put("{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}")))))
            .put("query-name", "visited-page-events"));
        JSONObject result = slicingdice.countEvent(queryData);
        System.out.println(result.toString());
    }
}
require 'rbslicer'
slicingdice = SlicingDice.new(master_key: 'MASTER_API_KEY')

query_data = [
    {
        "query" => [
            {
                "actions" => {
                    "equals" => "visited-page",
                    "between" => [
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}", 
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}"
                    ]
                }
            }
        ],
        "query-name" => "visited-page-events"
    }
]

puts slicingdice.count_event(query_data)

const SlicingDice = require('slicerjs');

const slicingdice = new SlicingDice({masterKey: 'MASTER_API_KEY'});

const queryData = [
    {
        "query": [
            {
                "actions": {
                    "equals": "visited-page",
                    "between": [
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}", 
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}"
                    ]
                }
            }
        ],
        "query-name": "visited-page-events"
    }
];

slicingdice.countEvent(queryData).then((resp) => {
    console.log(resp);
}, (err) => {
    console.error(err);
});

<?php
use Slicer\SlicingDice;
$slicingdice = new SlicingDice(array("masterKey" => "MASTER_API_KEY"));

$queryData = array(
    array(
        "query" => array(
            array(
                "actions" => array(
                    "equals" => "visited-page",
                    "between" => array(
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}", 
                        "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}"
                    )
                )
            )
        ),
        "query-name" => "visited-page-events"
    )
);

print_r($slicingdice->countEvent($queryData));
?>
using System.Collections.Generic;
using Slicer;
using Newtonsoft.Json;

namespace SlicerTester.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            var slicingdice = new SlicingDice(masterKey: "MASTER_API_KEY");

            var queryData = new List<dynamic>{
                new Dictionary<string, dynamic>{
                    {"query", new List<dynamic>{
                        new Dictionary<string, dynamic>{
                            {"actions", new Dictionary<string, dynamic>{
                                {"equals", "visited-page"},
                                {"between", new List<dynamic>{
                                    "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}", 
                                    "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}"
                                }}
                            }}
                        }
                    }},
                    {"query-name", "visited-page-events"}
                }
            };

            var result = slicingdice.CountEvent(queryData);
            System.Console.WriteLine(JsonConvert.SerializeObject(result).ToString());
        }
    }
}
package main
import (
    "fmt"
    "github.com/SlicingDice/slicingdice-go/slicingdice"
)
func main() {
    keys := new(slicingdice.APIKey)
    keys.MasterKey = "MASTER_API_KEY"
    slicingdice := slicingdice.New(keys, 60)

    queryData := []interface{}{
        map[string]interface{}{
            "query": []interface{}{
                map[string]interface{}{
                    "actions": map[string]interface{}{
                        "equals": "visited-page",
                        "between": []interface{}{
                            "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %}",
                            "{% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}",
                        },
                    },
                },
            },
            "query-name": "visited-page-events",
        },
    }

    fmt.Println(slicingdice.CountEvent(queryData))
}

The API response for this query above will be in the format below:

{
    "status": "success",
    "result": {
        "visited-page-events": 1
    },
    "took": 0.103
}

Let's understand each component of the API response:

  • status - Indicating whether the request was successfully processed or not.
  • result - Query name (visited-page-events) and total number of events that matches all query conditions.
  • took - Time spent processing this request, in seconds.

Query operators, predicates and parameters

This section is focused on API-based JSON endpoint if you're using SQL to make your queries feel free to skip this part.

Supported predicates

This is the list of supported predicates for the count events query.

equals

Usage example: How many events happened where the action was equals to Jeff?

gt or greater-than

Usage example: How many events happened where the purchase-value was greater than 50?"

gte or greater-than-or-equal

Usage example: How many events happened where the purchase-value was greater than or equal to 18?".

lt or less-than

Usage example: How many events happened where the purchase-value was less than 50?"

lte or less-than-or-equal

Usage example: How many events happened where the purchase-value was less than or equal to 21?".

starts-with

Selects only values that start with the requested substring.

Usage example: How many events have users that have a name that starts with j?

ends-with

Selects only values that end with the requested substring.

Usage example: How many events have products that have a name that ends with ona?

contains

Selects only values that contain the requested substring.

Usage example: How many events have countries that contain m?

not-contains

Selects only values that do not contain the requested substring.

Usage example: How many events have countries that not contains m?

between (required)

Usage example: How many events happened where the action was equal to "Pay Now" between the dates {% convert_date 'YYYY-MM-DDT00:00:00Z' '-15d' %} and {% convert_date 'YYYY-MM-DDT00:00:00Z' '-1d' %}?

Supported parameters

bypass-cache (optional)

Usage example: Boolean value defining to bypass or not the cache mechanism.

Use "bypass-cache": true to ignore cached data. bypass-cache is false by default, meaning that all queries will be cached.

query-name (optional)

You can send multiple queries in a same request, so when you send more than one query, these queries must be identified by unique names.

dimension (optional)

In which the query should be performed over.

If you do not provide a value to dimension, the query will be performed over the default dimension.

Supported operators

Currently, count events queries do not support any operators.


Query examples

You can check Count events query examples to see a lot of examples of count events queries.


What's Next

Aggregations