📜 ⬆️ ⬇️

Using Google Analytics in games

During the development of the game SUPERVERSE, we needed a tool to track how players interact with the game, as well as obtaining information about hardware, screen resolution, operating system, etc. This data could be useful not only at the debugging stage, but also would help to study the behavior of users in the game.



So, we had to choose one of three possible options:

• develop your own solution for tracking data on the game, client and server side;
• choose a third-party solution (for example, GameAnalytics );
• use already proven Google Analytics.
')
We felt that the implementation of the first two options would take more time and money, so we chose Google Analytics as a means to track game events and create reports.



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

Basic setting

To start tracking game events, follow a few simple steps:

• create a Google Analytics account if you don’t have one yet;
• configure a new tracking resource (you will receive an identifier that looks like this: UA-12345678-2);
• Start using the Measurement Protocol to send hits to Google Analytics via the HTTP protocol.

That's all.
Below you will find a step-by-step guide on using Google Analytics in computer games.

Data transfer

Data exchange in Google Analytics is done via GET and POST requests. The service supports secure transmission of data over HTTPS, but for this you can use plain HTTP. In this article, for simplicity, we will use as a model sending POST requests via an unprotected HTTP protocol. To monitor the events in the game, we used the library libCURL , which not only copes with the transfer of data, but also is suitable for other tasks. You can just as well send a POST request using the HTTP protocol by opening 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 should point to a string containing all the information to send to Google Analytics.

Each request for the transfer protocol of statistical data should contain the following values:



* In the future, the API may change. Follow the changes in the reference documentation of the statistical data transfer protocol.

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 for Google Analytics to determine that data is coming from a specific user, each client’s HTTP request must contain a client ID. This parameter is a universal unique identifier (uuid), which is assigned to a specific user. After creating an arbitrary uuid, you must save it in order to use it during the next launch of the game. Thus, Google Analytics will be able to identify a unique user, even if he hasn’t entered the game for some time.

Version of the game and operating system (useragent)

Google Analytics tracks browser type and version information. We can also use this feature to get information about the version of the game and the OS on which it is running. In this case, they will be registered in the line user-agent, for example:

Superverse/0.3 (Windows NT 6.2)


The Windows version number can be obtained by calling the GetVersionEx () function. For example, “Windows NT 6.2” means Windows 8.0.

Tracking data

By setting the basic parameters, you can start tracking the statistics you are interested in. The two most basic types of hits are pageview (page view) and event (event), but other options are possible: transaction (transaction), timing (time), social (social interaction), exception (exception) and item (product). You can find a detailed description of all types of complaints in the reference manual.

Session

To find out when a player entered the game and how much time he spent in it, you need to track the session data. To do this, add the following commands to the postdata line:

sc=start

sc=end


Session tracking can also be used to calculate the time a player spent in a particular match or game level.

Data sending frequency

Google determines how often the collected data is sent back to the server. As a rule, game statistics are updated no more than once every two seconds. In addition, one session involves no more than 500 calls, however, this is usually more than enough to monitor events in the game.

Conclusion

Event tracking is a simple asynchronous task that does not affect other components of the game.
It is recommended to create a separate buffer that will be filled with requests from the main stream of the game whenever you need to track any data. In addition, there must be a background thread that processes requests from the buffer and sends them to Google Analytics. Such a configuration of the service ensures that the frequency of sending does not violate the limitations and quotas of Google.

Source: https://habr.com/ru/post/273113/


All Articles