Get Interpretable Values with the Metadata API
the monetate metadata api allows you to retrieve interpretable values for experiences, page events, and custom targets monetate provides unique ids because they never change, whereas you can change the name of experience variants and page events in the platform you can send a request to the api with an experience id, page event id, or target id to return a set of interpretable values access and references your application must identify itself every time it sends a request to the monetate metadata api the monetate auth api allows you to manage tokens on a per user key basis with this api, you can request a token that has a fixed expiration, that is associated with a public key you uploaded to monetate, and that may be revoked by deactivating that key code example the following example script, in version 3 of python, demonstrates how to look up metadata for unique ids found in session stream \# coding utf 8 from collections import counter import json import jwt import requests import time \# we have here a sample session data file you can download one following the documentation for session stream with open('one hour json', 'r') as f raw data = f read() sessions = \[json loads(line) for line in raw data splitlines()] \# the session data in this file refers to objects in the platform like experiences, page events, and targets by id this means your data from session stream remains consistent regardless of name changes in the ui \# but to review this data, we want to turn these ids into human interpretable names the metadata api is the way to fetch those mappings \# metadata api requires authentication let's first get an auth token \# send a get request to the refresh endpoint with an 'authorization' header \# the value of the header must be 'jwt \<token>' where \<token> is an encoded jwt token as defined by rfc 7519 \# the token's payload must contain an 'iat' claim and a 'username' claim \# the 'iat' claim must contain a unix timestamp of when the request is sent a leeway of 60 seconds is allowed for client clock skew \# the api username used for the 'username' claim must have a public key configured in the ui (on the api keys tab of the sites page in the settings) \# the private key used to encode the jwt token must be the matching private key for the aforementioned public key \# create the payload in the 'username' claim, use the username that is generated by the api keys on the sites page of the ui settings payload = {'iat' int(time time()), 'username' 'api 111 demo'} \# load up your private key and use it to encode the payload we allow rs256, rs384, and rs512 as encoding algorithms with open('private pem', 'r') as f private key = f read() jwt token = jwt encode(payload, private key, algorithm='rs256') \# assemble and send the request to the auth endpoint \# the default ttl for the returned auth token is 3600 seconds (1 hour) \# you may optionally send a 'ttl' query parameter to request a specific ttl in seconds, between 600 (15 minutes) to 43200 (12 hours) auth refresh url = 'https //api monetate net/api/auth/v0/refresh/' resp = requests get(auth refresh url, headers = {'authorization' 'jwt {}' format(jwt token)}) \# grab the token from the response note that the response also contains an expiration timestamp so you can automate preemptive refreshing of tokens auth token = resp json()\['data']\['token'] \# now that we have a token, we can use it to access the metadata api \# replace "example" with your retailer shortname found in the base url of your api documentation metadata base url = \\ 'https //api monetate net/api/metadata/v1/example/production/metadata/' page event url = metadata base url + 'pageevent/' variant url = metadata base url + 'variant/' target url = metadata base url + 'customtarget/' \# let's go ahead and fetch a bunch of page events 1000 is the maximum allowed in one request \# these are paginated, so you can make smaller requests and piece them together later page event resp = requests get(page event url, headers = {'authorization' 'token {}' format(auth token)}, params = {'page size' 1000}) \# grab the list of events make it a dict so we can reference by id page event list = page event resp json()\['data'] page event data = {d\['id'] d for d in page event list} \# now let's marry this data to your session data from earlier \# session data has a property page event ids which is a dict of page event ids > the time at which the event occured \# these page event ids are the same ones retrieved in the last metadata api response \# let's loop over the sessions and print the city the user browsed in and triggered page events for the session missing page events = \[] for session data in sessions city = session data\['city'] session page events = \[] for event id in session data\['page event ids'] \# we'll need to coerce the ids back into ints since json forces dict keys to be strings try page event metadata = page event data\[int(event id)] \# let's grab the title session page events append(page event metadata\['title']) except keyerror missing page events append(event id) print('a user in {} triggered the following page events {}' format(city, session page events)) print('{} page events could not be matched; do you need to request more page event definitions from the metadata api?' format(counter(missing page events))) \# now let's grab experience data here, we'll also show how to do some simple pagination has next = true variant list = \[] page = 1 while has next variant resp = requests get(variant url, headers = {'authorization' 'token {}' format(auth token)}, params = {'page size' 1000, 'page' page}) parsed response = variant resp json() \# grab the list of experiences from this page variant list extend(parsed response\['data']) \# are there more pages to fetch? if parsed response\['meta'] get('next', none) page = page + 1 else has next = false \# make it a dict so we can reference by id variant data = {e\['id'] e for e in variant list} \# offer ids in our session data contain experience ids, which we've just fetched definitions for from the metadata api \# we'll do a simple counter to see what experiences were seen in the given day's worth of data experiences shown = \[] for session in sessions for offer id in session\['offers'] \# offer ids have an extra piece at the end indicating the variant, which you'll want to discard for our current purpose experience id = offer id split(' ')\[0] experiences shown append(experience id) experience counter = counter(experiences shown) \# now we can put the two together let's take the top 10 most seen experiences and display their information top 10 = experience counter most common(10) for experience id, count in top 10 print("{} was seen by {} sessions " format(variant data\[experience id]\['experience name'], count)) \# let's also fetch custom target metadata simply follow the same pattern target resp = requests get(target url, headers = {'authorization' 'token {}' format(auth token)}, params = {'page size' 1000}) \# grab the list of targets target list = page event resp json()\['data'] \# now let's make it a dict so we can reference by id target data = {d\['id'] d for d in target list} \# the above dict is keyed on custom target ids, which are present as keys in the custom targets dict on each session row \# you can marry the two just like we did above to do any of your own analytics