Hello Keen community!
We are happy to announce a new filter type — regex — to allow more complex searches on property names.
What is regex?
Regular expression (regex for short) is a string of text that allows you to create a match that helps you find a special combination of characters in text faster. It may seem like a different language at first, but mastering regular expressions can save you thousands of hours when working with large sets of data.
Simple examples of count and regex filter:
For example, to find a word in the string ‘Keen is the best platform for in-products analytics’
the matcher for the word ‘best’ is just ‘best’.
For the property name-value equal to ‘Aadnjsdnja-AAA-P12341’, refer to the code snippets below:
1. Using cURL:
curl https://api.keen.io/3.0/projects/PROJECT_ID/queries/count \
-H "Authorization: READ_KEY" \
-H 'Content-Type: application/json' \
-d "{
\"event_collection\": \"COLLECTION_NAME\",
\"timeframe\": \"this_14_days\",
\"filters\": [
{
\"property_name\" : \"PROPERTY_NAME\",
\"operator\" : \"regex\",
\"property_value\" : \"[A-Z][a-z]+-[A-Z]{3}-P[0-9]{1,16}\"
}
]
}"
regex cURL example
2. Using JavaScript
// Browsers
// import KeenAnalysis from 'keen-analysis';
// Node.js
const KeenAnalysis = require('keen-analysis');
const client = new KeenAnalysis({
projectId: 'PROJECT_ID',
readKey: 'READ_KEY',
});
client
.query({
analysisType: 'count',
eventCollection: 'COLLECTION_NAME',
filters: [
{
propertyName: 'PROPERTY_NAME',
operator: 'regex',
propertyValue: '[A-Z][a-z]+-[A-Z]{3}-P[0-9]{1,16}',
},
],
timeframe: 'this_14_days',
})
.then(res => {
// Handle results
})
.catch(err => {
// Handle errors
});
regex JavaScript example
3. Using Java
KeenProject keenProject = new KeenProject(PROJECT_ID, WRITE_KEY, READ_KEY);
KeenQueryClient keenQueryClient = new KeenQueryClient.Builder(keenProject)
.build();
Query query = new Query.Builder(QueryType.COUNT)
.withEventCollection(COLLECTION_NAME)
.withTimeframe(new RelativeTimeframe("this_14_days"))
.withTimezone("UTC")
.withFilter(PROPERTY_NAME, FilterOperator.REGEX, "[A-Z][a-z]+-[A-Z]{3}-P[0-9]{1,16}")
.build();
QueryResult result = keenQueryClient.execute(query);
if (result.isLong()) {
long queryResult = result.longValue();
}
regex Java example
To match strings generated by UUID for example, ‘123e4567-e89b-12d3-a456-426614174000’:
^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[0-9]{12}$
^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$
^[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}$
^\w{8}(-\w{4}){3}-\w{12}$
1. Using cURL:
curl https://staging-api.keen.io/3.0/projects/PROJECT_ID/queries/count \
-H "Authorization: READ_KEY" \
-H 'Content-Type: application/json' \
-d "{
\"event_collection\": \"regex-2\",
\"timeframe\": \"this_14_days\",
\"filters\": [
{
\"property_name\" : \"regex-2\",
\"operator\" : \"regex\",
\"property_value\" : \"^\\\w{8}(-\\\w{4}){3}-\\\w{12}$\"
}
]
}" | python -m json.tool
regex cURL example
2. Using JavaScript
import KeenAnalysis from 'keen-analysis';
const client = new KeenAnalysis({
projectId: 'PROJECT_ID',
readKey: 'READ_KEY',
});
client
.query({
analysisType: 'count',
eventCollection: 'COLLECTION_NAME',
filters: [
{
propertyName: 'PROPERTY_NAME',
operator: 'regex',
propertyValue: '^\\w{8}(-\\w{4}){3}-\\w{12}$',
},
],
timeframe: 'this_14_days',
})
.then(res => {
// Handle results
})
.catch(err => {
// Handle errors
});
regex JavaScript example
3. Using Java
KeenProject keenProject = new KeenProject(PROJECT_ID, WRITE_KEY, READ_KEY);
KeenQueryClient keenQueryClient = new KeenQueryClient.Builder(keenProject)
.build();
Query query = new Query.Builder(QueryType.COUNT)
.withEventCollection(COLLECTION_NAME)
.withTimeframe(new RelativeTimeframe("this_14_days"))
.withTimezone("UTC")
.withFilter(PROPERTY_NAME, FilterOperator.REGEX, "^\\w{8}(-\\w{4}){3}-\\w{12}$")
.build();
QueryResult result = keenQueryClient.execute(query);
if (result.isLong()) {
long queryResult = result.longValue();
System.out.println(queryResult);
}
regex Java example
The final result is a number type.
Thank you for reading! Please reach out with any feedback or questions.