Developers Club geek daily blog

1 year, 11 months ago
During development of the game SUPERVERSE we needed means for tracking of how players interact with game, and also receipts of data on "iron", a display resolution, operating system, etc. These data could be useful not only at a debugging stage, but also would help to study features of behavior of users in game.

Use of Google Analytics in games

So, we needed to select one of three possible options:

• to develop own solution for tracking of data on the party of game, the client and the server;
• to select a third-party solution (for example, GameAnalytics);
• to use already proved Analytics Google service.

We considered that on implementation of the first two options more time and means therefore stopped on Google Analytics as means for tracking of game events and creation of reports will leave.

Use of Google Analytics in games

Recently Google Analytics is actively used by developers of websites and mobile applications. But this service does not cause special enthusiasm as is not a convenient solution in the form of SDK with a possibility of complete integration into the project in creators of computer software, as in a case with applications on iOS and Android.

Basic setup

To begin to trace game events, execute several simple steps:

• create the account Google Analytics if you do not have it yet;
• configure a new resource of tracking (at the same time you get the ID which looks so: UA-12345678-2);
• begin to use the transfer protocol of statistical data (Measurement Protocol) to transfer addresses to Google Analytics under the HTTP protocol.

Here, actually, and all.
Further you will find the step-by-step tutorial on use of a Google service of Analytics in computer games.

Data transmission

Data exchange in Google Analytics is performed by means of GET-and POST requests. Service supports safe data transmission under the HTTPS protocol, but for this purpose it is possible to use also normal HTTP. In this article we, for simplicity, will use as model sending POST requests under the unprotected HTTP protocol. For monitoring of events in game we used libCURL library which not only perfectly copes with data transmission, but also is suitable for a solution of other tasks. Equally well it is possible to send POST request under the HTTP protocol, having opened a TCP socket on port 80.

void Send2GoogleAnalytics (char * postdata, char * useragent)
{
CURL * curl_handle = curl_easy_init ();
if (curl_handle) {
curl_easy_setopt (curl_handle, CURLOPT_URL, "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);
}
}


PostData

The postdata variable has to indicate a line which contains all information for sending to Google Analytics.

Each request of the transfer protocol of statistical data has to contain the following values:

Use of Google Analytics in games

* In further API can change. You monitor changes in the reference documentation of the transfer protocol of statistical data.

Total, the description of the postdata parameter will look as follows:

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

User identification (UUID)

In order that Google Analytics could define that data arrive from the specific user, in each HTTP request has to be specified the client's identifier. This parameter represents a universal unique identifier (uuid) which is appropriated to the specific user. After creation of any uuid it needs to be saved to use during next run of game. Thus, Google Analytics will be able to define the unique user even if it any time did not come into game.

Version of game and operating system (useragent)

Google Analytics traces information on the type/version of the browser and an operating system. We can also use this function to receive data on the version of game and OS on which it is started. At the same time they will be registered in the line user-agent, for example:

Superverse/0.3 (Windows NT 6.2)

The version number of Windows can be learned by function call of GetVersionEx (). For example, "Windows NT 6.2" designates Windows 8.0.

These trackings

Having configured key parameters, you can begin to trace the statistical data interesting you. Two most base types of addresses are pageview (viewing of the page) and event (event), but also other options are possible: transaction (transaction), timing (time), social (social interaction), exception (exception) and item (goods). It is possible to find the detailed description of all types of addresses in a reference guide.

Session

To learn when the player came into game and what is the time carried out in it, it is necessary to trace data on session. For this purpose add the following commands to the line postdata:

sc=start sc=end

Tracking of sessions can also be used for calculation of time which the player spent in a specific match or level of game.

Frequency of sending data

Google defines how often collected data go back to the server. As a rule, the game statistics is updated not more often than time in two seconds. Besides, one session assumes no more than 500 addresses, however and usually more than it is enough of it for monitoring of events in game.

Output

Tracking of events is the simple asynchronous task which is not influencing other components of game.
It is recommended to create the separate buffer which will be filled with requests from the main flow of game every time when you need to trace any data. Besides, there has to be background a flow processing requests from the buffer and sending them to Google Analytics. Similar setup of work of service guarantees that the frequency of sending does not break restriction and a quota of Google.

This article is a translation of the original post at habrahabr.ru/post/273113/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: sysmagazine.com@gmail.com.

We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus