📜 ⬆️ ⬇️

Google Analytics with Unity3D



At some point in the development of your game project there is a need to collect statistics on it. After all, this will allow us to understand what a player does, how a player thinks while using our game and will give us the opportunity to refine some moments, point us to things that annoy a player, show that he likes and much more.

And here comes the choice of which analytics system to choose. Our choice is quite decent: our own development of Unity, Google Analytics, Appat, Flurry, Localitycs - and this is only a small part of the systems that are available to us. Who wants to know for what reason the analytics system from Google was chosen and how to work with it, please under the cat.

System selection


So, we see such a large variety of analytics systems, but I have taken Google Analytics, so why?
')
There are a lot of reasons and here are the main ones :

And now we can sort things out by points so that it becomes much clearer :


Now it’s pretty clear what this analytics system was chosen from, and now we can proceed to the plug-in integration phase in our application.

Integrating Google Analytics into a project


The first thing to do is download our plugin from the repository and this is what we see:



We are only interested in a few things: this is actually GoogleAnalyticsV4.unitypackage, the file from the iOS Extras folder and from the source folder is the library for Android.

So, importing files from Google Analytics V4.unitypackage, upload the file from the iOS Extras folder to the Editor folder in the Assets root, this file will be needed for compiling for iOS, which I’ll tell you about later and from the source / Plugins folder copy the Android folder to the Plugins folder, without this The library will simply not work on the Android platform.

And now a little about compiling for iOS. The file that we transferred from iOSExtras is necessary for the libraries needed for the plugin to be automatically connected, but we also need mod_pbxproj.py, which is also downloaded from the repository, the link to which is in the Google Analytics documentation. It also needs to be placed in the Editor folder. Now all the necessary libraries will be connected automatically, but you need to take into account that this will work only when you are compiling from under Mac, otherwise the libraries will need to be manually connected to the project .

Libraries to connect :


To set it up for Android, we still need to modify the manifest file, adding the following lines after the tag:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 


With the import of files into the project and their configuration is finished, now let's move on to the settings inside the project and start with the prefab GAv4.prefab, which is located along this path:


I will tell only about the basic settings that we need and these are:


Android Traking Code - the code of the application that you receive, when creating an application in Google Analytics, you can use both the code for the application that you created in your Unity account and purely for Android
IOS Traking Code - the code of the application that you receive when creating an application in Google Analytics, you can use both the code for the application that you created in your Unity office and purely under IOS
OtherTraking Code - the code of the application that you receive, when creating an application in Google Analytics, you can use only the code for the application that you created in your office under Unity
Product Name - the name of the product that you specified when creating the application
Bundgle Identifire - application identifier by type com.company.name
Bundle Version - application version

A little bit about tracking codes. You can create one application to track immediately from all platforms, or you can create a separate application for each platform. Codes have the form UA-XXXXXXX-1.

These are the basic settings that need to be specified, the rest can be gathered from the documentation, because they are not so critical for the operation of the plugin.

It remains only to transfer the prefab to the scene and you can get to work. Everything is pretty simple here, I’ll give just one script as an example that describes the whole essence of working with Google Analitycs.

 using UnityEngine; public class Analitycs : MonoBehaviour { public GoogleAnalyticsV4 componentAnalitycs; //    void Start () { //  componentAnalitycs.StartSession ();//  componentAnalitycs.LogEvent ("Default Category", "Default Action", "Default Description", 0L); //   componentAnalitycs.LogException ("Description", false);//   componentAnalitycs.LogItem ("ID", "Name", "SKU", "Category", 1.99, 0L);//      componentAnalitycs.LogScreen ("Menu");//  ,        componentAnalitycs.LogSocial ("FaceBook", "Rep", "Friends");//    componentAnalitycs.LogTiming ("Category", 0L, "Time Game", "Time");// ,         componentAnalitycs.LogTransaction ("ID", "Affil", 19.99, 1.00, 0.00);//       componentAnalitycs.StopSession (); //  } } 


That's all, now you can safely work with the analytics system. If it’s interesting to work specifically with Google Analytics personal account, I can write a separate article, but I don’t think what I need, everything is painfully simple and intuitive.

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


All Articles