Google Analytics Mobile Plugin [Released] [Free]

NOTE: Google Analytics has officially released a similar Unity plugin today, only 2 years after we released ours :wink: GitHub - googleanalytics/google-analytics-plugin-for-unity: Google Analytics plugin for the Unity game creation system

Hey guys - we at Mass Threat recently launched 50 Ways to Survive on iOS/Android and are pleased to report it’s already a top ranking app in many countries! We couldn’t have done it without Unity3D and its awesome community of developers who share their knowledge and hard work with others.

During the dev process for 50 Ways we created a simple lightweight plugin to report data to Google Analytics, in order to receive realtime stats about our app after we launched. Flurry is a great solution to mobile analytics, but it’s missing that oh-so-cool realtime element.

Free Download:
GoogleAnalyticsPlugin.unitypackage - v0.1

So, what is it?

In 2012 Google Analytics introduced support for Mobile Apps (iOS / Android), which offers all of the great features of web analytics but with nomenclature suitable for mobile and a concept of “screen views” and events as opposed to webpage visits and the like. Google released great SDKs for both iOS and Android, and we’ve been using them with much success in our non-game applications.

However - despite offering great SDKs for native iOS and Android apps, there was no good way to use Google Analytics Mobile within a Unity3D game. We have built a plugin that allows you to easily report what screen your players are on within Unity3D, using one line of code. Our solution is very lightweight and utilizes Unity’s native WWW class to report the data to Google.

This is a plugin for Google Analytics more recent addition of “App” analytics. You must create an app property in Google Analytics, not a regular website property.

At Mass Threat we utilize a range of analytical tools in our games, including Flurry - and while Flurry works great for collecting event-based data, generating reports and quickly finding gameplay trends, it does lack certain features that we had grown so accustom to when managing our web-based properties. For example, Flurry does not have native support for city / state-level geographic data (you would need to perform your own user location lookup, and report the data to Flurry), nor does it have the type of neat realtime stats we’ve grown to love so much with Google Analytics.

Because our implementation of Google Analytics is lightweight and only allows you to log screen views, we would still recommend using Flurry as a supplemental solution. Having Google Analytics in our games affords the ability to quickly glance and see realtime player data (who’s doing what) and a much more granular look at geographic trends. We still utilize Flurry to gather information about player hardware and other more complex metrics.

Installing the Plugin

Download the Unity Package from the above URL, and import it to your project. “GoogleAnalytics.cs” contains everything you need to get started reporting your data to Google. In order to initialize the plugin, create an empty GameObject in your Unity3D scene and call it “GoogleAnalyticsManager” or similar. Next (this is the important part), attach the GoogleAnalytics.cs script to your empty GameObject.

The GameObject you place this script on will persist throughout the session, which makes the plugin available across all scenes. Take care to add the GameObject to a loading scene or initialization scene, so that it will be ready for usage across your entire game.

Once you’ve added the script to your GameObject, you will be able to configure the script to match your Analytics credentials.

Property ID - You can find your property ID by visiting the “Admin” panel of your application within Google Analytics.
Bundle ID - Specify your bundle ID here.
App Name - Specify the name of your application - the same as would appear beneath your game’s icon on a player’s homescreen.
App Version - Your current app version.

Implementation / Usage

Once you’ve setup the plugin as described above, logging screens to Google Analytics is easy. The code below will report that the user is on the “Main Menu” screen.

// Reports that the user is viewing the Main Menu
    if (GoogleAnalytics.instance)
    GoogleAnalytics.instance.LogScreen("Main Menu");

Think about what your game consists of in terms of “screens” - in our case we normally log each gameplay level as a screen (i.e., “Level 12”), and also log anytime a GUI dialog of any meaning is displayed (i.e., “Level 12 - Player Death” or “Powerups Menu”)

The Result

Once setup, you will be able to access great realtime data such as users online, geographic location and more. We’ve found that it’s much easier to identify player trends (and possible problems) by monitoring realtime usage.

Known Limitations

  • iOS traffic (iPhones/iPods/iPads) will show as “Desktop”, while Android traffic will be accurately separated into either Mobile or Tablet. For reliable device stats, we utilize Flurry.
  • As mentioned above, for analytical heavy lifting we still utilize Flurry, as it handles metrics in an industry-standard way. Additionally, Flurry will gather data from offline devices and report it once the user goes online. This plugin does not.
  • This plugin will report the viewing of “Screens”, it does not currently have a method for “Events” (events will accept key/value data, whereas Screens accept a single string) - again, for deep event data we recommend using a full-fledged mobile analytics suite like Flurry.

Bookmarked! :slight_smile:

Very Nice!:smile:
We just need it !

I managed to integrate the native Android SDK into Unity. Wrote all about it here. I will soon start taming the iOS SDK.

For now I send screen, events and timings. This is enough for me, but it’s easy to add even more types.

Thanks to everyone for downloading over 300 times so far! :slight_smile:

@oferei - cool! You’re exactly right. Our approach was to do something super lightweight and cross-platform without actually dealing with SDKs.

@DavidJNels - Do we need the analytics sdk in our xcode project? And will this work on a development build to test? Or only once the app goes live?

@rainbladestudios - No SDK necessary. It’ll work on the development build.

Guys, this seems awesome. I was waiting for someone to implement the new Google app analytics.
Downloading and installing right this instance.

Thanks for sharing your work.

@DannyB - no problem! Enjoy.

@DavidJNels - Thanks for getting back to me. Seems awesome. Is there a certain amount of time that needs to pass before the stats start showing up?

@Rainbladestudios - Great! If you go to Realtime in Google Analytics, your activity should be showing up instantly (it looks like in the screenshot in my post) … make sure you’re using a Google Analytics mobile app profile, and not a regular GA website profile.

Cool. So do I need to do anything to the actual script? I filled out the public variables for the name, bundle id, property id, and version in the editor. Is there anything else I need to do? Besides calling the logscreen code?

That’s it! Just make sure the Google Analytics script (where you filled out the public vars) is on a GameObject that will persist throughout your game - so your main menu or initial loading screen.

Thank you! I will give it a shot and see what happens.

Ok, so this works perfectly.

I have created a modified version of it, with these changes.

  1. No need to create any object, it will run staticly (e.g. just need to call GoogleAnalytics.LogScreen() )
  2. Title argument is optional (defaults to Application.loadedLevelName)
  3. Can enable or disable execution in the editor

I would very much like to extend this more, to support events and whatever else is possible to send (for example, if the Google API supports direct sending of device type).
Where did you find the documentation about which parameters to place in the URL? I only found Google’s SDK

@DannyB - Very nice! Finding out the URL parameters was a matter of using a packet sniffer on Android to see the requests and then recreate them.

Well, as I am android-less, if you are up for it, and you can determine what is the URL needed for event tracking (or other nice options that the API provides) along with some clear field markers (e.g. DUMMY_EVENT, DUMMY_CATEGORY), I can extend the code and share my results back.

( Also, I think it would be preferred if you provide the download as a CS file and not a UnityPackage. Since its one file, there is no real need to package it. )

Thanks. I’ll try to look into it this week and post my results.

You’re right about the UnityPackage - I think originally I had a prefab for the GoogleAnalyticsManager GameObject in there too, which justified it a bit. If you wanna share your modifications (shoot me a PM) I’d be glad to put out a new 0.2 version and credit you in the notes!

So I got it in, no errors, I have it logging that it is calling the function to report the screen, but it is not showing up in my Analytics account. I am building to an iPad, and running a dev build. Any ideas what could be going on? Could it be google taking its time to report back to me? I set up my account 2 days ago…

Are you certain you’re using a Google Analytics mobile app account / profile? As opposed to a normal Google Analytics website profile.

In the upper left of the GA header, does it show a little phone icon and say “All Mobile App Data” on their website? See attached

1310859--61524--$Screen Shot 2013-07-26 at 2.43.53 PM.png