This article has been featured by GamaSutra.
A version in Serbian language has been published by StartIt.rs

During the development of SUPERVERSE game we needed a way to track how the players interact with the game, what kind of hardware are they playing on, screen resolutions, operating system and other similar information. This kind of data will be handy for debugging purposes but also for examining behaviors and patterns in player interactions.

Of course, we needed a solution for this and the choice was between these three options:

  • Develop our own tracking solution from scratch on both the game, client side, and the back-end side.
  • Choose a 3rd party solution (e.g. GameAnalytics).
  • Adopt Google Analytics we all know and love.

First two options seemed more costly in terms of time or money needed to have them running, so instead, we decided to try using Google Analytics to track and report game events.

Using Google Analytics has been quite popular for the websites and mobile apps lately but this is not the case with the desktop PC software. The reason for this is most probably the lack of an easy-to-use pre-packaged solution in the form of SDK that you could just plug into your project, such is the case for iOS and Android apps.

Basic setup

In order to start tracking events from your game you need to:

  • Setup the Google Analytics account if you don’t have one yet.
  • Setup a new tracking property, thus get a tracking ID – it looks like this: UA-12345678-2.
  • Use Measurement Protocol and send hits to Google Analytics using HTTP.

That’s it.

Or if that was not detailed enough for you, read on for step by step tutorial on how to add Google Analytics tracking into your PC game.

Communication

Sending data to Google Analytics can be achieved using either GET or POST requests. Google Analytics allows secure data transfers by relying on HTTPS protocol but also plain HTTP can be used. For the matter of simplicity, we’ll be using unsecured HTTP POST requests in this text. For our game we’ve utilized libCURL as it handles communication well and we’ll be eventually using it for some other tasks. Opening a TCP socket on port 80 and sending an HTTP POST request shall do the job equally well.

void Send2GoogleAnalytics(char *postdata, char *useragent)
{
  CURL *curl_handle = curl_easy_init();
  if ( curl_handle ) {
    curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.google-analytics.com/collect");
    curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, useragent);
    curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, postdata);

    curl_easy_perform(curl_handle);

    curl_easy_cleanup(curl_handle);
  }
}

Post data (postdata)

postdata variable should point to the string that contains all the information you want to send to Google Analytics.

Required information for every Measurement Protocol request are:

Name Parameter Example Description
Protocol Version v v=1 The protocol version. The value should be 1.
Tracking ID tid tid=UA-123456-1 The ID that distinguishes to which Google Analytics property to send data.
Client ID cid cid=xxxxx An ID unique to a particular user.
Hit Type t t=pageview The type of interaction collected for a particular user.

* API may change in the future, please consult Measurement Protocol reference documentation

So, the exemplar postdata may look like:

postdata = "v=1&tid=UA-123456-1&cid=UUID&t=pageview&dp=%2FStart%20screen";

User identification (UUID)

In order for Google Analytics to figure out that the data is coming from the single user, you need to send client ID parameter with each HTTP request. This parameter expects the universally unique identifier (uuid) and you can generate one using code like this. Once you generate a random uuid you should store it and use that uuid next time the game is started. This will make Google Analytics know that the player currently playing is the same one that played the game last week – even if he quit the game in the meantime.

Game version and operating system (useragent)

Google Analytics tracks the browsers and their version numbers as well as the operating system the browser is working on. We can use this to track our game’s version number and the system it’s running on by providing this information in the form the user agent string, e.g:

Superverse/0.3 (Windows NT 6.2)

Windows system version number can be obtained by calling GetVersionEx(). “Windows NT 6.2” in the example actually marks Windows 8.0.

Tracking data

Once you have those basics in place you can start sending Google Analytics information of interest for your measurements and tracking. Two most basic types of hits are pageview and event, but there are others such as transaction, timing, social, exception and item. Each of those is well documented in the reference guide.

Session

In order to see when the player started the game and how long her played it, you should start a session and end it before she leaves the game. This is achieved by appending following commands to postdata string:

sc=start
sc=end

Sessions could also be used to mark the beginning and the end of player playing the match or the level within the game.

Reporting frequency

Google defines how often data can be transmitted to them. The game should not be sending data more often than once in two seconds. In addition to that, the number of events is limited to 500 hits per session. However, those limitations should be more than enough for tracking happenings in your game.

Putting it all together

Tracking events should be a seamless and asynchronous task that has no impact on other components of the game.

Suggested solution would be to have a buffer of requests that’s being filled from the main thread of the game whenever you like to track some information. On the other hand, there should be a background thread that processes the buffered requests and sends them to Google Analytics, making sure the sending rate is acceptable to Google and doesn’t break the limits and quotas they have in place.