Segment Create

Create a segment.

See /api/segment/validatemethod for testing out Segmentation rules.

Pass a Segment QL logic expression to createa 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, use * 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"
)


Language
Credentials
Header
Click Try It! to start a request and see the response here!