Day 4: Run in production + API

Gijs Nelissen
Solving Marketing Attribution
4 min readApr 25, 2020

Today we’ll run the app in production and provide an API to allow us to query by userId and anonymousId

Create basic API

Add these functions to your serverless.yml

getAnonymous:
handler: src/handlers/api/getAnonymous.handler
events:
- http: get /api/anonymous/{id}
getUser:
handler: src/handlers/api/getUser.handler
events:
- http: get /api/user/{id}

This will make the serverless.yml file look like this

Then we need the two handlers for those API functions. Notice how we create src/handler/api a directory which we’ll use to move our handlers over time.

All the code to handle the API is already there (we added it in Day 3) so the handlers are pretty simple:

/src/handlers/api/getUser.js
/src/handlers/api/getAnonymous.js

Deploy this with a quick sls deploy

> sls deployServerless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service solving-marketing-attribution.zip file to S3 (10.52 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.......................................................................
Serverless: Stack update finished...
Service Information
service: solving-marketing-attribution
stage: dev
region: eu-west-1
stack: solving-marketing-attribution-dev
resources: 38
api keys:
None
endpoints:
POST - [$BASE_URL]/events
GET - [$BASE_URL]/api/anonymous/{id}
GET - [$BASE_URL/api/user/{id}
functions:
hello: solving-marketing-attribution-dev-hello
processPage: solving-marketing-attribution-dev-processPage
processIdentify: solving-marketing-attribution-dev-processIdentify
getAnonymous: solving-marketing-attribution-dev-getAnonymous
getUser: solving-marketing-attribution-dev-getUser
layers:
None
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

Testing the API and Event Processing

To test the attribution and anonymous to userId mapping a little better let’s feed the system some events. I have created some sample events (all JSON files). So download those and put them in the /events folder.

Archive of segment events (5 in total).

mkdir events && cd events
wget https://ucarecdn.com/a75fc569-fcd0-4671-bd4f-2c93110234ca/sample_events.zip
unzip sample_events.zip
cd ..

Now make sure all your dynamoDB sma tables are empty and run the following command to POST each event in one go

// easy way (all in one go)
for d in ./events/* ; do (http POST $BASE_DOMAIN/events < "$d" ); done
// hard way (one by one)
http POST $BASE_DOMAIN/events < events/anon_page_1.json
http POST $BASE_DOMAIN/events < events/anon_page_2.json
...

You will now be able to query users by anonymousId or userId and get a list of their sessions and referral sources.

Testing by anonymousId

http GET $BASE_DOMAIN/api/anonymous/da8011b3-19bf-4d3d-b1b6-caa961cb5161

Should give you something like this:

And testing by userId

http GET $BASE_DOMAIN/api/user/john@gmail.com

Should return something like this

So let’s see what we have here? This is the response coming from the /user API

One UserId with 3 sessions from different sources

Looks good. Not sure how I am going to digest/use that data and pass it back to segment but let’s consider that a problem for later.

Running in production

So we’re ready to test drive this in production. Open your serverless.yml and change the stage to prod

Quick sls deployshould create new tables, new event streams and new functions.

Created sma-*-prod tables

When the deployment is done search for the function that is linked to the POST endpoint (URL ending with /events). Copy that URL and log in to your segment account.

Go to your project > destinations > webhooks and enable the webhook to this URL.

Enable the webhook for this url

After that you can go to the event tester to see if all works well

200 Success 😎

--

--

Gijs Nelissen
Solving Marketing Attribution

Belgian Techie. Builder. Bootstrapper. Dad x3. Entrepreneur. Smarty pants. Passionate about the web & technology. Founder of @prezly