Microservice Recipe: Web Analytics

Service Recipes > Web Analytics
04 Mar 2017

Develop highly scalable custom web analytics solutions with microservices.

There are many web analytics solutions available such as Google Analytics, Mixpanel, Kissmetrics etc, each offering rich feature sets. You may however simply be looking for a much simpler lightweight solution, or have specific custom requirements that cannot be met by an off-the-shelf package. Eventn makes it incredibly simple to develop massively scalable microservices for analytics, offering complete flexibility over features and functions. In fact, to create a basic web analytics service requires no server-side code at all!

To test this out, create a new Eventn service and simply add the following client side JavaScript to a web page you wish to track so that it is executes when a page is visited e.g. within a footer component. Remember to replace the service name and token accordingly:

fetch("https://service.eventn.com/[--service--]", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer [--service-token--]"
    },
    body: JSON.stringify({
      data: {
        path: window.location.pathname,
        userAgent: navigator.userAgent
      }
    })
  })
  .then(response => response.json())
  .then(json => console.log(json.status))
  .catch(err => console.log('Error:', err));

With the client side code in place, visit the desired web page and a request will made executing the Eventn POST function, saving the path and user agent. Executing aGETrequest against the service will return the number of records stored.

One-click Microservice

With the above example, we can see how easy it is to start collecting browser data at scale. Lets look at exactly what is happening here and how it can be extended.

Creating an Eventn microservices is a simple as a single click within the user interface, or a single api call (POST /service). Each microservice provides the following features:

  • An secure, internet facing REST resource accessible athttps://service.eventn.com/[--service-name--]
  • A sandboxed JavaScript GET function that is executed by making an HTTP GET request
  • A sandboxed JavaScript POST function executed by making an HTTP POST request
  • A MySQL datastoretable used for data persistence.

The automatic coupling of a MySQL database table to each microservice provides a fast way to deploy a new service, without setting up your own storage. The default table structure is simple providing a timestamp and a JSON field for data storage. Read more about the default schema. Likewise, each service provides default functions for collection (POST) and retrieving a record count (GET). Read more about the default functions.

Client Side HTTP Requests

With the microservice in place, its simply a case of make the POST request using an HTTP library of choice such as jQuery (jQuery.post()) or as in this case, the native (see browser support and polyfillFetch API is used.

In the example provided, both the pathname and userAgent are collected, with the latter using theWindow.navigatorproperty. This is useful for browser detection etc

Next Steps

Clearly this is a very simple example, but hopefully useful in demonstrating how a scalable custom web analytics system can easily be developed. Custom data stores can be used to connect Eventn so your own databases where specific properties can be stored as required utilizing appropriate indexes etc. Finally, reporting on the analytics within a graphical dashboard would utilize theGETfunction. Using request parameters (seerequest.query), multiple SQL queries can be configured using a single microservice.

Get Started For Free

Get Started