This article was contributed by Mark Edmondson and Peter Meyer, both from IIH Nordic, specialists in online marketing and web communication headquartered in Copenhagen, Denmark. Read more about the authors in the bottom of the article.
Many analytics specialists agree that Machine Learning is going to revolutionise the digital analytics industry in the future, as all the major vendors battle to provide Machine Learning APIs that offer to surface interesting features of your data. These APIs offer cloud solutions that you can use to both scale up your own models, or take advantage of pre-trained models.
Following our presentation at Superweek Hungary 2017
, we wanted to show how you could start using these services today to enhance your own digital analytics capabilities.
Among all Machine Learning techniques, we chose Sentiment Analysis since it is a common use case, but the same code with small modifications could be used for any of the machine learning APIs offered by the services we chose, Algorithmia and Google Natural Language API. For those not acquainted with Sentiment Analysis this YouTube video
goes into detail about one of the approaches we use below.
Case Study: Sentiment Analysis
Our use case was built for a large news website that allowed comments on their articles. We wanted to be able to monitor in real-time the reaction to the articles as they were published, as judged by the user comments beneath the article. This could then be used in the newsroom to decide which articles to highlight or demote on the homepage.
Another considered use case was for a large brand who carried user forums. A live measurement of the general sentiment on the forums under certain products could indicate when a problem or trend was starting with that product, and help inform marketing or customer support if there was a pending issue.
Maybe your users feel that the product support you provide could be better, find your products very hard to use, or have a hard time finding out how to best provide you feedback on your services. Wouldn't it be very interesting to look at numbers providing you exactly that information?
In the past, it may have taken an expensive 3rd party tool or offline analysis to get the Sentiment Analysis, but as we show below, it isn't very complicated to do it yourself, directly into your web analytics, as they happen.
The services used are both paid services, so please take that into account before using any of them for anything else but testing.
The proof of concept website
This demo was originally created for a presentation at Superweek Hungary in Feb 2017. The presentation, which includes additional examples is embedded below.
You can find the demo website at https://bit.ly/Superweek2017-Demo
- it takes the code we produce below and creates a small webapp to compare the results. [Bear in mind that if it goes over the free tier it may not work until the tier resets the next day
This site is a flat HTML page, which has Google Tag Manager deployed to provide the sentiment analysis output from the two platforms tested: Algorithmia and Google Cloud Platform. You just enter a text in the big text area, click the blue button, and then get Sentiment Analysis results returned in the two output boxes below the button. Feel free to check it out :-)
The machine learning API services
is a cool service: a marketplace with thousands of machine learning algorithms for you to choose from. The algorithms are standardised so you can then use the same code library to run it where you need to. At present, their marketplace main categories are:
- Text Analysis
- Machine Learning
- Computer Vision
- Deep Learning
The algorithm we chose is the Stanford NLP Sentiment Analysis
, which returns one value: the sentiment, which spans from 0 (Very Negative) to 4 (Very Positive).
Because Algorithmia serves as an algorithm marketplace, you can try out several before deciding which one is best for you. And if you want to, you can even put your own algorithms onto the marketplace to sell alongside the others.
Google Cloud Platform
Google currently offers a lot of different cloud products
, including the Machine Learning services, and the Cloud Natural Language API
. The API includes functionality for analysing entities, sentiment and syntax. Today we focus on just the sentiment part, but exactly the same approach could be used to classify topics from text.
The Sentiment Analysis service from Google provides two output values: polarity and magnitude. Polarity is a value between -1 (Negative) and 1 (Positive), and magnitude is a value between 0 (Neutral) and infinite (Strongly/Clearly). A nice description of this approach can be found in the article Sentiment analysis on tweets
by Guillaume Laforge. Here is a descriptive illustration from the article:
Deploying Sentiment Analysis through Google Tag Manager
, but the same approach could work for other tag management systems. You may also choose to deploy them straight onto the webpage, but a tag manager was useful for testing concepts quickly.
Follow along with screenshots and code in this GitHub repository
Examples of both services have been adapted from the relevant documentation for each service.
The following GTM tag does exactly that, in this case the include files are put in the HEAD section of the page (code
Calling the Algorithmia API
Once the submit button is clicked, the script below is then used to send the text typed by the user into the form, (code
Briefly, that's what the script does:
- Checks for the existence of the Algorithmia file
- Sends Algorithmia the text from the input form
- Receives the response and pushes a GTM event
Once the event is returned, the API response is shown on the page like this.
To convert the numeric value returned by the service and translate it into a more verbose value, we created a lookup table below.
Calling Google Cloud Natural Language API
Bear in mind this service is currently in beta and so the code below may not work if the API changes significantly in the future.
A similar script is used when calling the Google API (code
Briefly, that's what the script does:
- Registers with the Natural Language API
- Calls the API with the form text
- Upon getting an answer, pushes a GTM event
The returned output is then displayed on the page like this:
Sending output to Google Analytics
We display the results in the demo application, but the sentiment scores can just as easily be sent straight into Google Analytics for analysis as an event. This then lets you pull out the results via the real-time or reporting APIs for your needs. Below is an example GTM code to do this.
Sending Algorithmia sentiment value to Google Analytics
In the tag below the text value ("Positive", "Negative" etc.) is sent as the label, and the sentiment score as a value. Adjust to your needs.
Sending Google sentiment value to Google Analytics
In this case, the Event label is the magnitude (0 - 1) and the value is the polarity (-1, +1). You may prefer to make another variable that multiplies the magnitude and polarity together to create one value.
You have now seen two approaches on how to implement Sentiment Analysis on a web page. The examples are quite simple, but hopefully they show how quickly you can get up and running with Sentiment Analysis on things like user comments, forum entries, and support requests, and perhaps inspire you to try other applications.
This also goes to show that you don't have to know a lot about Machine Learning, AI, Deep Learning, algorithms and such, to get started reaping the benefits of what it can do to help you. Though we do urge you to learn more about it if this catches your interest :-)
And if you do have the knowledge to create your own Machine Learning algorithms, both platforms offer a way to deploy that model at scale through almost exactly the same mechanism: Google through Tensorflow
and Algorithmia through its marketplace.
We hope this triggered your interest in the subject. Feel free to get in touch if you are inspired to make anything cool on top of it, we'd love to hear from you!
About the Authors
is an Englishman in Denmark, where he lives and works with data. He grew up in Cornwall, UK, where he started his career in digital marketing in 2007, after completing his Physics masters. These days he splits his time working with digital analytics data for IIH Nordic and working with Google APIs as part of his role as a Google Developer Expert for Google Analytics. Outside work he enjoys composing music in the bunker and being a Dad. Mark blogs at code.markedmondson.me
and enjoys twittering at @HoloMarkeD
has been in the digital industry since 1997, beginning with HTML, moving over front- and backend development, SQL databases and Sitecore CMS, to now implement analytics and TMS products like Google Analytics, Adobe Analytics, Tealium IQ, Ensighten Manage and Google Tag Manager. Learn more about him on LinkedIn
or follow him on Twitter @pmeyerdk