Segment
Segments are named, logical expressions
of users. These segments may logically be built
using other segments as well. The segment api
provides a list of segments built by both the admin, pre-defined
segments automatically added to each account as well as custom ones
created through the UI.
Segments represent a number of concepts:
* *Segment* A list of users, users are members (or not) of a segment
* *Trigger* As a user enters, or leaves a segment, this is a trigger.
Triggers do not have a Size, only a history of events.
* *Goal* A segment that also implies conversion. In Lytics
App, there are *Goals* that show more prominently in a funnel
type interface.
* *Dynamic Attribute* A user belonging to a segment describes an attribute
about that user. For instance *IsPurchaser* is both an attribute
that can help personaliation, as well as a segment. We call these
special segments *Aspects* at Lytics.
* *Metric* A segment when used exclusively for the purposes of
tracking conversions/metric.
The Segment resource has the following attributes:
| field | DataType | Description |
|-------------|------------------|---------------|
| id | string | unique id, Lytics assigned
| created | Date | Created date
| updated | Date | Last Updated Date
| kind | string | What Type of segment from list above ["aspect","goal"] or leave empty for "segment".
| slug | string | customer-assigned unique id (We auto assigned by slugifying Name if not provided).
| name | string | Name of this segment
| description | string | Long text description (optional)
| is_public | bool | Is this segment available as part of user profile api (entity api)
| segment_ql | string | Segment QL Logical Expression
| tags | []string | List of tags, for organization purposes
To see a list of available fields available for segmentation rules see **catalog** api.
SegmentList
https://api.lytics.io/api/segmentGet list of **Segments** . Segments can refer to any Table.
The two main tables you will be working with are *User* and *Content*.
``
# Grab all segments from "user" table
curl "$LIOAPI/api/segment" -s -H "Authorization: $LIOKEY" | jq '.'
# Grab all "Content Collections" (ney, segments)
curl "$LIOAPI/api/segment?table=content" -s -H "Authorization: $LIOKEY" | jq '.'
``
{
"data": [
{
"id": "7a7f76ff97ad0e3e96c162f74aee8f8f",
"account_id": "4f7b525bdba058fc096757a6",
"author_id": "user_12345",
"kind": "segment",
"segment_ql": "FILTER AND ( visits > 5, last_visit >= \"2015-04-01 00:00:00Z\", last_visit < \"2015-04-02 00:00:00Z\")",
"created": "2014-08-04T21:18:20.124Z",
"updated": "2014-08-04T21:18:20.124Z",
"description": "Casual users have been active in the last 90 days and show no extreme behavioral patterns.",
"is_public": true,
"name": "Recent Visits",
"slug_name": "recent_visits",
"tags": [
"email_trigger"
]
}
]
}Segment Create
https://api.lytics.io/api/segmentCreate A segment. See **/api/segment/validate**
method for testing out Segmentation rules.
Pass a **Segment QL** logic expression to create
a Segment.
``
# Plain Text api will extract the "our_test_activelist" as the "slug" and "name" of
# the segment
curl -s -XPOST "https://api.lytics.io/api/segment" \
-H "Content-type: text/plain" \
-H "Authorization: $LIOKEY" \
-d'
FILTER AND (
visits > 5,
last_visit >= "now-30d",
scores.momentum > 10
)
ALIAS our_test_activelist
' | jq '.'
# or try managing segments in files so you can check them in
# to Bitbucket or Github. Also addresses field-
curl -s -XPOST 'https://api.lytics.io/api/segment' \
-H 'Content-type: text/plain' \
-H "Authorization: your_api_token" \
-d @my_segment.json | jq '.'
# Json Api Allows a few more fields such as description
curl -s -XPOST "https://api.lytics.io/api/segment" \
-H "Authorization: $LIOKEY" \
-H "Content-type: application/json" \
-d'
{
"name":"Most Active Users",
"segment_ql": "FILTER AND (visits > 5,last_visit >= \"now-30d\", scores.momentum > 10)",
"slug_name":"our_test_activelist",
"is_public": true,
"description":"a description of what this is for",
"tags":["email_personalization","product"]
}
' | jq '.'
# create a json file of a segment
curl -s -XPOST "$LIOAPI/api/segment" \
-H "Authorization: $LIOKEY" \
-H "Content-type: application/json" \
-d @segment.json | jq '.'
# Content Collection (ney, "Segment")
curl -s -XPOST "https://api.lytics.io/api/segment" \
-H "Content-type: text/plain" \
-H "Authorization: $LIOKEY" \
-d'
FILTER AND (
EXISTS imageurls -- Make sure they have images
aspects = "article" -- Ensure they are of type article
PATH = "blog" -- only show those from the /blog part of site
created > "now-30d" -- only those authored in last 30 days
)
FROM content
WITH
name = "Recent Blog Articles With Images"
ALIAS recent_blog_articles
' | jq '.'
`
SegmentQL
=============================
The query language for segments
`
Filter = "FILTER" Phrase [FROM] [ALIAS]
Phrase = AND | OR | Expression
AND = "AND" (Phrase, Phrase, ...)
OR = "OR" (Phrase, Phrase, ...)
Expression = NOT
| Comparison
| EXISTS
| IN
| CONTAINS
| LIKE
| IncludeSegment
NOT = "NOT" Phrase
Comparison = Identifier ComparisonOp Literal
ComparisonOp = ">" | ">=" | "<" | "<=" | "==" | "!="
EXISTS = "EXISTS" Identifier
IN = Identifier "IN" (Literal, Literal, ...)
CONTAINS = Identifier "CONTAINS" Literal
LIKE = Identifier "LIKE" String # uses * for wildcards
IncludeSegment = "INCLUDE" Identifier
# Optional From, not needed for segmentation on users
FROM = "FROM" Identifier
# Alias for giving a segment a "name" which is how
# it will be included from other filters
ALIAS = "ALIAS" Identifier
Literal = String | Int | Float | Bool | Timestamp
# note, identifiers may not contain spaces, or periods, etc
Identifier = [a-zA-Z][a-zA-Z0-9_]+
`
**Examples**
`
# Simple single expression filter
FILTER "abc" IN some_identifier
FILTER NOT foo
# Filters have an optional "Alias" used for
# referencing
FILTER AND ( channelsct > 1 AND scores.quantity > 20 ) ALIAS multi_channel_active
# Filters can references to other Filters
FILTER AND (
EXISTS email,
NOT INCLUDE multi_channel_active
)
FILTER AND (
visits > 5,
NOT INCLUDE someotherfilter,
)
# negation
FILTER NOT AND ( ... )
# Compound filter
FILTER AND (
visits > 5,
last_visit >= "2015-04-01 00:00:00Z",
last_visit < "2015-04-02 00:00:00Z",
)
# Like
FILTER url LIKE "/blog/"
# date math
# Operator is either + or -. Units supported are y (year), M (month),
# w (week), d (date), h (hour), m (minute), and s (second)
FILTER last_visit > "now-24h"
# IN
city IN ("Portland, OR", "Seattle, WA", "Newark, NJ")
# Complex
AND (
OR (
foo == true
bar != 5
)
EXISTS signup_date
OR (
NOT bar IN (1, 2, 4, 5)
INCLUDE SomeOtherFilter
)
)
# Time Windows
# Can only be used on timebucket fields
# Answers the question "at least X in Y days", where X can be one of (1, 5, 10, 25, 50, 100)
# and Y can be one of (7, 30) days.
#
# timewindow(fieldname, X, Y)
FILTER timewindow(bucketfield, 5, 7)
# Between
FILTER AND (
_modified BETWEEN "2015-07-01" AND "2016-08-01"
)
``
{
"data": {
"id": "7a7f76ff97ad0e3e96c162f74aee8f8f",
"account_id": "4f7b525bdba058fc096757a6",
"author_id": "4f7b525bdba058fc096757a6",
"kind": "segment",
"segment_ql": "FILTER AND ( visits > 5, last_visit >= \"2015-04-01 00:00:00Z\", last_visit < \"2015-04-02 00:00:00Z\")",
"created": "2014-08-04T21:18:20.124Z",
"updated": "2014-08-04T21:18:20.124Z",
"description": "Casual users have been active in the last 90 days and show no extreme behavioral patterns.",
"name": "Recent Visits",
"slug_name": "recent_visits",
"tags": [
"email_trigger"
]
}
}Segment Fetch
https://api.lytics.io/api/segment/{id}Fetch a segment by **id** or **slug**
{
"data": {
"id": "7a7f76ff97ad0e3e96c162f74aee8f8f",
"account_id": "4f7b525bdba058fc096757a6",
"author_id": "4f7b525bdba058fc096757a6",
"kind": "segment",
"segment_ql": "FILTER AND ( visits > 5, last_visit >= \"2015-04-01 00:00:00Z\", last_visit < \"2015-04-02 00:00:00Z\")",
"created": "2014-08-04T21:18:20.124Z",
"updated": "2014-08-04T21:18:20.124Z",
"description": "Casual users have been active in the last 90 days and show no extreme behavioral patterns.",
"name": "Recent Visits",
"slug_name": "recent_visits",
"tags": [
"email_trigger"
]
}
}Segment Update
https://api.lytics.io/api/segment/{id}Update A segment.
``
# You can upsert (insert if new, update if exists)
# by POSTing to api and the ALIAS will be used to lookup
# the existing one and update it.
curl -s -XPOST "https://api.lytics.io/api/segment" \
-H "Content-type: text/plain" \
-H "Authorization: $LIOKEY" \
-d'
FILTER AND (
visits > 5,
last_visit >= "now-30d",
scores.momentum > 10
)
ALIAS our_test_activelist
' | jq '.'
# Json Api Allows a few more fields such as description
curl -s -XPUT "https://api.lytics.io/api/segment/123456id" \
-H "Authorization: $LIOKEY" \
-H "Content-type: application/json" \
-d'
{
"name":"Most Active Users",
"segment_ql": "FILTER AND (visits > 5,last_visit >= \"now-30d\", scores.momentum > 10)",
"slug_name":"our_test_activelist",
"is_public": true,
"description":"a description of what this is for"
}
' | jq '.'
``
{
"data": {
"id": "7a7f76ff97ad0e3e96c162f74aee8f8f",
"account_id": "4f7b525bdba058fc096757a6",
"author_id": "4f7b525bdba058fc096757a6",
"kind": "segment",
"segment_ql": "FILTER AND ( visits > 5, last_visit >= \"2015-04-01 00:00:00Z\", last_visit < \"2015-04-02 00:00:00Z\")",
"created": "2014-08-04T21:18:20.124Z",
"updated": "2014-08-04T21:18:20.124Z",
"description": "Casual users have been active in the last 90 days and show no extreme behavioral patterns.",
"name": "Recent Visits",
"slug_name": "recent_visits",
"tags": [
"email_trigger"
]
}
}Segment Delete
https://api.lytics.io/api/segment/{id}Delete A segment.
No Content
SegmentSize
https://api.lytics.io/api/segment/sizeGet size of a single Segment using SegmentQL, not saved.
This api is very rate throttled, 1/10 seconds.
``
# Post a SegmentQL query to size api to fetch size
curl -s -XPOST "https://api.lytics.io/api/segment/size" \
-H "Content-type: text/plain" \
-H "Authorization: your_api_token" \
-d'
FILTER AND (
visits > 5,
last_visit >= "now-30d",
scores.momentum > 10
)
' | jq '.'
``
{
"data": {
"id": "",
"name": "active",
"slug_name": "active",
"size": 10402
}
}SegmentValidate
https://api.lytics.io/api/segment/validateValidate single Segment Query Statement
{
"status": 200,
"message": "success",
"data": null
}SegmentSizes
https://api.lytics.io/api/segment/sizesGet bunch of segment sizes.
{
"data": [
{
"id": "456dert",
"name": "Active",
"slug_name": "active",
"size": 10402
},
{
"id": "1234abcd",
"type": "custom",
"slug_name": "active",
"size": 45236
}
]
}SegmentScan
https://api.lytics.io/api/segment/{segId}/scanGet a paged list of **Users** for a **Segment**.
null
Segment Batch Delete
https://api.lytics.io/api/segment/{segId}/scanDelete a Batch of segments
``
# ids=1234 convert this to []string{"123"}
# ids=[123,456] convert this to []string{"123","456"}
# ids=123,456 convert this to []string{"123","456"}
# ids=123&ids=456 convert this to []string{"123","456"}
# ids[]=123&ids[]=456 convert this to []string{"123","456"} Note that we alias ids[] = ids
curl -s -XDELETE -H "Content-Type: application/json" \
- H "Authorization: $LIOKEY" \
"https://api.lytics.io/api/segment?ids=21decdea54052ea1285478ecac53d1ed,59e39389fbf738d75246749a6146d280" \
| jq '.'
``
No Content
SegmentAttribution
https://api.lytics.io/api/segment/{segId}/attributionGet single attribution
{
"data": [
{
"id": "1234",
"metrics": [
{
"ts": "1413149713466",
"value": 1002
},
{
"ts": "1413149713455",
"value": 1005
}
]
},
{
"id": "1111",
"metrics": [
{
"ts": "1413149713466",
"value": 1002
},
{
"ts": "1413149713455",
"value": 1005
}
]
}
]
}SegmentAttributionList
https://api.lytics.io/api/segment/attributionGet bunch of segment sizes
{
"data": [
{
"id": "1234",
"metrics": [
{
"ts": "1413149713466",
"value": 1002
},
{
"ts": "1413149713455",
"value": 1005
}
]
},
{
"id": "1111",
"metrics": [
{
"ts": "1413149713466",
"value": 1002
},
{
"ts": "1413149713455",
"value": 1005
}
]
}
]
}SegmentFieldInfo
https://api.lytics.io/api/segment/{segId}/fieldinfoGet field info by segment
null