Trending
Opinion: How will Project 2025 impact game developers?
The Heritage Foundation's manifesto for the possible next administration could do great harm to many, including large portions of the game development community.
Featured Blog | This community-written post highlights the best of what the game industry has to offer. Read more like it on the Game Developer Blogs or learn how to Submit Your Own Blog Post
How to use Google Analytics and track events, usage patterns and other interesting information in a PC game.
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 similar informations. This kind of data will be handy for debugging purposes but also for examining behaviours 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 web sites 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 easy-to-use pre-packaged solution in the form of SDK that you could just plug in to your project, such is the case for iOS and Android apps.
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 enough detailed for you read on for step by step tutorial how to add Google Analytics tracking into your PC game.
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, "h t t p : / / w w w . g o o g l e - a n a l y t i c s . c o m / c o l l e c t";);
// PLEASE REMOVE THE SPACES FROM URL ABOVE - THOSE WERE ADDED AS WORKAROUND TO GAMASUTRA'S EDITOR THAT MESSES UP THE REST OF TEXT AFTER URL
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);
}
}
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";
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.
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.
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.
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 beginning and end of player playing the match or the level within the game.
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.
Tracking events should be 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.
Read more about:
Featured BlogsYou May Also Like