/
How to create a Contextual segment and activate it
How to create a Contextual segment and activate it
This example bash script uses curl to send requests and jq to parse responses.
# Here we're using the Basic auth method to make the example easier to read. This requires a
# specific service account user. To use a personal user, you need to authenticate using OpenID
# instead.
#
# The API will return appropriate 4xx HTTP status codes if there are errors, but we are not handling
# them here (because it's hard to do with curl). It would be simple to check that returned IDs are
# not empty, as error responses do not contain an "id" field.
export BASE_URI='<https://epsilon-prod-api.weborama.com'>
# Get the list of projects (folders) to find the one we want to create the segment in. Beware that
# multiple projects may exist with the same name, especially if they are not located in the same
# parent project. Otherwise, you can use null or 0 as folder_id to create a segment in the root
# project. If this command finds a project, PROJECT_ID will contain its ID; otherwise it will be
# empty.
export PROJECT_ID=$(curl -u $USERNAME:$PASSWORD $BASE_URI/api/contextual-projects | jq '.[] | select(.name == "My Project") | .id')
# Alternatively, you can create a project and use its ID. The previous command can give you the ID
# of the desired parent project; or, again, use null or 0 as parent_id to create the new project in
# the root project.
export PROJECT_ID=$(curl -u $USERNAME:$PASSWORD -XPOST -d'{"parent_id": null, "name": "My New Project"}' $BASE_URI/api/contextual-projects | jq '.id')
# Now we create a Contextual segment, using language_id = 1 for English (you can double check this
# at /api/languages). This segment is composed of a whitelist containing two words and a blacklist
# containing one. We set SEGMENT_ID to the new Contextual segment's ID.
export WORD_LISTS='{"whitelistA": [{"word": "apple"}, {"word": "pear"}], "whitelistB": [], "blacklist": [{"word": "banana"}]'
export SEGMENT_ID=$(curl -u $USERNAME:$PASSWORD -XPOST -d'{"name": "My New Segment", "folder_id": $PROJECT_ID, "language_id": 1, "query": '$WORD_LISTS'}' $BASE_URI/api/contextual-segments | jq '.id')
# We can modify the segment contents later if we change our minds about apples. Note that endpoints
# for a single element resource use singular nouns; plurals are reserved for collections.
export WORD_LISTS='{"whitelistA": [{"word": "pear"}], "whitelistB": [], "blacklist": [{"word": "apple"}, {"word": "banana"}]'
curl -u $USERNAME:$PASSWORD -XPATCH -d'{"query": '$WORD_LISTS'}' $BASE_URI/api/contextual-segment/$SEGMENT_ID
# Finally, create a Contextual streaming job, using the segment ID, in order to "activate" it. Here
# we activate a single segment, but it is possible to provide multiple segment IDs at once, which
# will cause one job to be created for each segment ID provided. If the account has multiple targets
# that it can export profiles to, /api/contextual-targets will list them; here we hardcode the
# target ID. Threshold 1 means "Max Reach", down to 4 for "Engagement". This endpoint returns an
# array of job creation results, so we're looking at the first element only. It will be an object
# with fields "result" (should be "ok" for a successful creation), "contextual_segment_id" (normally
# equal to $SEGMENT_ID), and "job_id" (the new job's ID).
curl -u $USERNAME:$PASSWORD -XPOST -d'{"contextual_segment_ids": ['$SEGMENT_ID'], "contextual_target_id": 1, "threshold": 1}' $BASE_URI/api/contextual-streaming-jobs | jq '.[0]'
, multiple selections available,