Serverless Twitter Analytics with CosmosDB and Logic Apps
Tags: cosmosdb, socialmedia, nosql, cloud
Published: 2018-05-17 14:47:45

---
title: Serverless Twitter Analytics with CosmosDB and Logic Apps
published: true
tags: cosmosdb,social-media,nosql,cloud
canonical_url: https://blog.jeremylikness.com/serverless-twitter-analytics-with-cosmosdb-and-logic-apps-280e5ff6c948
---

#### Using the Azure cloud to understand click behavior

I recently added a [Logic App](https://jlik.me/cly) to my Azure portfolio, and the insights it provides are amazing. I chose Logic Apps because I needed an [easy way to query Twitter](https://jlik.me/clz). In the Logic Apps designer, I simply clicked “Twitter”, logged in, and specified my search. It does the rest. I didn’t have to write a line of code or even worry about the nuances of authentication. It was all handled for me!

![](https://cdn-images-1.medium.com/max/1024/1*WSt3jUojaiUJO2s2sYc2Aw.png)<figcaption>Logic App workflow</figcaption>

It’s been several months and tens of thousands of clicks since I started using my [custom link shortener](https://blog.jeremylikness.com/build-a-serverless-link-shortener-with-analytics-faster-than-finishing-your-latte-8c094bb1df2c). I use this tool daily and leverage the analytics to refine the information I send out to social media. The goal is simply to find out how developers react to various topics so that I can eliminate the ones that are not interesting and focus on the ones that generate interest and excitement. In the context of the tool, a “click” is a “vote” for a particular topic.

I can look at the past 24 hours, for example, and see that although [Twitter](https://twitter.com/JeremyLikness)is by far the most active social medium I use, there is plenty of activity from my [Channel 9 videos](https://jlik.me/b10), this blog, and even [GitHub](https://github.com/JeremyLikness).

![](https://cdn-images-1.medium.com/max/990/1*shlWX-xPO84GfcAivtO_7g.png)<figcaption>Redirects by medium (pasts 24 hours) on January 18, 2018</figcaption>

I also started tracking data like the target host pages. This gives me an interesting visual of where most of the clicks through my URL tracker end up.

![](https://cdn-images-1.medium.com/max/1024/1*NOvRM6hTUAJTq_m45NuQgA.png)<figcaption>Clicks by host</figcaption>

Recently, I realized that I was missing some important information. Although it’s not consistent, some sources are kind enough to provide referral information. Every request also provides a user agent. Combined, this information gives me valuable insights such as the browsers being used, whether my content is read on mobile or desktop, and where the clicks originate.

I’ll get into more of the user agent code in a different blog post, but adding referral information to the tracking data was as simple as:

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0967f935dc53a0c99b29c3489d6239f/href">https://medium.com/media/c0967f935dc53a0c99b29c3489d6239f/href</a></iframe>

This gives me both a page and a host so I can organize data at different levels. I love that [CosmosDB](https://jlik.me/cl0)is a schema-less database, so it’s trivial to add new properties as an afterthought. I just ignore the old data in my analytics that need referral information. After running with the new information for awhile, I noticed an interesting trend.

![](https://cdn-images-1.medium.com/max/388/1*KzSc2-Bha91m6lEY2MXlLA.png)<figcaption>Referring hosts</figcaption>

A large number of referrals were coming from “t.co” which is Twitter’s own link shortener. In fact, I realized that every new tweet gets a unique “short link.” This means that I can search Twitter for the exact short link URL, and find the original tweet that the referral came from! This is useful to me to analyze what tweets generate the most engagement, and to “fact check” my counts compared to Twitter’s stats.

> Note: it would be great to understand activity at a retweet level, but this is not possible. A retweet simply references the original tweet, so there is no way of determining whether the click came from the original or a retweet. Therefore, I set up the logic to track the original tweet information, and from there I can always query statistics from twitter about likes and retweets.

I knew that Logic Apps would be the easiest way to process the Tweets, so I logged into my [Azure portal](https://jlik.me/cl1) and added a new Logic App. The designer makes it incredibly easy to connect to various resources and assets. Here is the first part of my workflow. The trigger is a timer I run every two hours and will tweak as needed based on activity. I initialize a variable to represent the current document and the Twitter link, then grab all documents that have a Twitter referral but haven’t yet been mapped to the original tweet.

![](https://cdn-images-1.medium.com/max/836/1*Su8fnMySZVdJbFbvIVclWg.png)<figcaption>Grabbing documents for processing</figcaption>

Notice the flexibility of the SQL syntax in being able to find a document that doesn’t have a property defined yet (not(is\_defined(…))). CosmosDB automatically indexes all properties, so these queries execute blazing fast.

I add a “for…each” loop to iterate the resulting documents.

![](https://cdn-images-1.medium.com/max/920/1*uWWgvHbQ2zV2SV5s6iSZWg.png)<figcaption>Iterate over each document</figcaption>

I set the “document id” variable to the current id, and the “twitter URL” variable to the Twitter website. This is the default I’ll use in case the search turns up empty.

> Note: Logic Apps by default runs for loops in parallel. Although this is great for scale, the process I’m writing is sequential because I need to map the document directly to the link I find. Therefore, I set the parallelism under “settings” to “1.” I may refactor this in the future if I learn a way to scope the variables to the parallel process.

If you’re curious, here is a sample document that has a Twitter referral but hasn’t yet been mapped to the original tweet.

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/954ec844c6b7921e354ce4ff837f3d2f/href">https://medium.com/media/954ec844c6b7921e354ce4ff837f3d2f/href</a></iframe>

Now I search Twitter for tweets containing the link.

![](https://cdn-images-1.medium.com/max/824/1*U7KVZGb7A8Ms-bTcXbNioA.png)<figcaption>Search tweets</figcaption>

I only need one result. I also modify the referral URL a bit because sometimes there is a querystring attached that I don’t need. Here is the backing code for the step.

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f05920a212187a2d2efac89e9dca6034/href">https://medium.com/media/f05920a212187a2d2efac89e9dca6034/href</a></iframe>

I then iterate the results (which at most I’ll get one) and branch based on whether or not an “original tweet” exists.

![](https://cdn-images-1.medium.com/max/1024/1*dn3Tfr1SEy-5xHcCyj-uIA.png)<figcaption>Branch based on original tweet</figcaption>

The variables are set either using the original tweet or the current tweet. “Original tweet” is a nested object, so the easiest way to check for its existence is to coalesce with a known value. Here is the expression I compare to the text literal “null”:

coalesce(items('For\_each\_2')['OriginalTweet'], 'null')

The twitter URL variable now contains one of three possible values:

1. The Twitter URL because the search did not return results
2. The Original Tweet because the search returned a retweet
3. The Tweet itself because the search returned an original tweet

The next step involves adding the new property to the document. There isn’t a direct connector for this in Logic Apps, but Logic Apps play well with [Azure Functions](https://jlik.me/cl2) and adding the function was trivial:

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ceca2b6700e9aeed21b0fe74a36c847/href">https://medium.com/media/5ceca2b6700e9aeed21b0fe74a36c847/href</a></iframe>

Functions make life so easy! By providing a [CosmosDB (DocumentDB interface) binding](https://jlik.me/cl3), I don’t have to do anything to retrieve the document. The binding automatically retrieves the id from the header, finds the corresponding document (if it exists) and then passes it into the function method. All I need to do is pull the URL that is passed as a simple text string as the body of the post and add it as a new referralTweet property to the document. The binding even does the update for me!

There are two ways to call functions from Logic Apps. You can connect to certain functions directly, or you can call the function endpoint as a client. I chose the latter.

![](https://cdn-images-1.medium.com/max/817/1*2PhYQXYBrZmamNGhEFbvoQ.png)<figcaption>POST to the function endpoint</figcaption>

Notice I use the variables to add the document id to the route and include the twitter URL in the body. The design allows me to run this any time to capture unprocessed documents, and already is providing me with new insights on my dashboard.

![](https://cdn-images-1.medium.com/max/592/1*qBV2IKIGkTMSwtyoCCjGSA.png)<figcaption>Referring tweets</figcaption>

Of course, now that I have the tweet, I can take it a step further and grab the tweet text to show more context or even a preview on the dashboard. But that’s a task for a different day!

If you haven’t already tapped into the power of Logic Apps, I encourage you to [check them out](https://jlik.me/cly) today.

Until next time,

![](https://cdn-images-1.medium.com/max/197/1*KwXn3ElS7vnWIk8lNVhBsA.gif)

* * *