Day 4 - Run in production + API
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
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:
There is really nothing to it setting up this quick controller that gets an argument (userId or anonymousId), looks for it in the DynamoDB and responds with results (if any)
🔗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).
Now make sure all your dynamoDB sma tables are empty and run the following command to POST each event in one go
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
Should give you something like this
And now another test by userId
Will output some more results
So let’s see what we have here? This is the response coming from the /user API
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 deploy should create new tables, new event streams and new functions.
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.
After that you can go to the event tester to see if all works well
Now let’s let this integration run for a few days (it’s weekend after all) and check in on Monday if there are any issues/problems we need to solve.
Other articles in the series
05/07/2021
Day 11 - Sales Attribution
03/07/2021
Day 10 - Six months later
03/06/2020
Day 9 - Dealing with tracking/ad blockers
18/05/2020
Day 8 - Feeding in sales data
06/05/2020
Day 7 - Reporting on visitor sources
01/05/2020
Day 6 - Feeding source attribution data back to Segment.com
27/04/2020
Day 5 - Feed old events
24/04/2020
Day 4 - Run in production + API
22/04/2020
Day 3 - Cleanup & Identify Visitor Source
21/04/2020
Day 2 - Capture segment events
20/04/2020
Day 1 - The Masterplan
19/04/2020
Solving marketing attribution (using segment)