Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Google Analytics Mobile Plugin [Released] [Free]

Discussion in 'Assets and Asset Store' started by DavidJNels, Jun 29, 2013.

  1. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    NOTE: Google Analytics has officially released a similar Unity plugin today, only 2 years after we released ours ;) https://github.com/googleanalytics/google-analytics-plugin-for-unity

    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.

    Code (csharp):
    1. // Reports that the user is viewing the Main Menu
    2.     if (GoogleAnalytics.instance)
    3.     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.
     
    Last edited: Sep 25, 2014
  2. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,820
    Bookmarked! :)
     
  3. kevinlan

    kevinlan

    Joined:
    Jul 3, 2013
    Posts:
    2
    Very Nice!:D
    We just need it !
     
  4. oferei

    oferei

    Joined:
    Dec 1, 2012
    Posts:
    36
    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.
     
  5. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    Thanks to everyone for downloading over 300 times so far! :)

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

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    @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?
     
  7. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    @rainbladestudios - No SDK necessary. It'll work on the development build.
     
  8. DannyB

    DannyB

    Joined:
    Jun 20, 2012
    Posts:
    214
    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.
     
  9. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    @DannyB - no problem! Enjoy.
     
  10. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    @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?
     
  11. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    @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.
     
  12. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    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?
     
  13. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    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.
     
  14. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    Thank you! I will give it a shot and see what happens.
     
  15. DannyB

    DannyB

    Joined:
    Jun 20, 2012
    Posts:
    214
    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
     
  16. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    @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.
     
  17. DannyB

    DannyB

    Joined:
    Jun 20, 2012
    Posts:
    214
    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. )
     
  18. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    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!
     
  19. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    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...
     
  20. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    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

    $Screen Shot 2013-07-26 at 2.43.53 PM.png
     
  21. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    It does indeed. And the name above All Mobile App Data needs to match the name of the app on itunesconnect right?
     
  22. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    It should but not sure that'd matter. If you're not seeing your activity on the realtime view then I think that has to mean there's something wrong with the property ID you entered in the script on Unity3d.

    It should follow this format UA-XXXXXXX-X
     
  23. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    Maybe I am confused. Do I need to change anything in the actual script? Or just in the editor? All my variables match in the editor
     
  24. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    Mine has 8 X's between the dashes. Could that be it?
     
  25. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    Yes I was referring to the script vars in the Editor. Shouldn't be any required changes inside the script itself.

    However if you wanna troubleshoot, I'd suggest adding a Debug.Log("test") inside LogScreen to make sure your game is actually calling the plugin and making the request to Google correctly...

    EDIT: The 8 digits are fine, I've seen others with that many.
     
  26. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    I have a debug.log in there, and it is calling the function. Is there a way to test where it is being sent? Is the commented out code that is in the script something I can use to test this?
     
  27. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    Yes, uncomment the Debug.Log(url) and if you could please PM what you get to me, I'll check it out. :)
     
  28. rainbladestudios

    rainbladestudios

    Joined:
    Sep 13, 2012
    Posts:
    18
    Alright! Got it working. Not sure what it was, (most likely user error ha ha), but it's now working like a charm thanks to @DavidJNels and his wonderful support.
     
  29. haroonalam

    haroonalam

    Joined:
    Sep 1, 2012
    Posts:
    4
    Hi, I can see the user stats in App Overview, but there is no data in Real-Time reports. Is there anything I am missing..
     
    Last edited: Sep 10, 2013
  30. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    I'm not sure what might be causing that other than something that needs to be tweaked in your setup process. If you'd like you can PM me the code and variables you're using.
     
  31. haroonalam

    haroonalam

    Joined:
    Sep 1, 2012
    Posts:
    4
    I can see now the real time analytics, I guess it takes delay from google for 12-24 hrs. Thanks for implementation and support
     
  32. b0wnie

    b0wnie

    Joined:
    Jun 14, 2013
    Posts:
    5
    Excellent and very straightforward to use. Do allow 24 hours for your Google Analytics mobile profile to activate. A great way of getting simple analytics into Unity with a minimum of fuss.
     
  33. iamIcarus

    iamIcarus

    Joined:
    Jul 3, 2013
    Posts:
    20
    i have the same problem that no data are recorded to the Analytics App Account.
    I double checked about the profile , it is for Mobile App and the tracking info is correct

    i see that this issue is resolved to others , but i cannot see the solution ?

    can you please help?
     
  34. tonic

    tonic

    Joined:
    Oct 31, 2012
    Posts:
    439
    I hope you don't mind if I mention my solution for integrating Google Analytics to Unity projects:

    Google Universal Analytics for Unity in Asset Store.
    (forum thread here)

    Google's new Universal Analytics has a "measurement protocol" specification, and the above package uses that to send hits to analytics. The advantage here is that there's no need for native library for each platform. So the same code actually works on mobile, desktop and web player platforms. It's implemented in pure C# and uses Unity's WWW class for networking.

    Contrary to the package you're offering, this one isn't free, but in comparison it does work on more platforms, offers complete API to the analytics offered by the measurement protocol specs (e.g. also supports events), and it also supports offline caching for hits to send them later (including a built-in internet accessibility monitoring).

    More info in the Google Universal Analytics for Unity home page.
     
    Last edited: Jul 22, 2014
  35. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
    Hi David,

    This is an awesome post and "EXACTLY" what I was hoping to find.

    I've imported the assest, attached it to a gameobject, filled in the vars including Property ID of a Google mobile app, but and I'm not seeing any action on the Google Analytics site?

    Any hints would be appreciated.

    Thank you again for this post, it's is very generous.

    UPDATED 12/3/13

    It looks as though I need to wait the 24 hours for Google to activate "whatever", so please disregard this request, if tomorrow Google is still not reporting I may ask again.

    Thank you very much David!

    UPDATED 12/4/13 (morning ~ 8:30am pst)

    Hi David,

    Thank you for your reply last night!

    Well, it's been about 22 hours and the analytics are still absent.
    I looked at the "GoogleAnalytics" script and there was a portion that was commented out right after "WWW request = new WWW(url)" that had to do with request.error so I thought I would uncomment it and see if it gave me any new info.

    It followed the path - if(request.error == null) - so one would think that it's good ???

    Here is an exception that was logged however:

    UnityException: WWW is not finished downloading yet
    UnityEngine.WWW.get_responseHeaders () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/Utils.cs:155)
    GoogleAnalytics.LogScreen (System.String title) (at Assets/Plugins/GoogleAnalytics/GoogleAnalytics.cs:50)
    LoginSelect.Do_pnl_LoginSelect (.dfControl ignore, .dfMouseEventArgs args) (at Assets/Scripts/LoginSelect.cs:100)
    dfControl.OnClick (.dfMouseEventArgs args)
    dfControl.OnClick (.dfMouseEventArgs args)
    dfButton.OnClick (.dfMouseEventArgs args)
    dfInputManager+. (IInputAdapter , Ray , .dfControl , Boolean )
    dfInputManager. ()
    dfInputManager.Update ()

    Here is the code at line 50:
    if (request.responseHeaders.ContainsKey("STATUS"))


    and I'm using the following code snippet at LoginSelect.cs:100

    if (GoogleAnalytics.instance){
    GoogleAnalytics.instance.LogScreen("btn_FindGame");

    I sure hope this is useful information.

    Any ideas?

    Thank you.

    UPDATED 12/4/13 2:44pm pst

    Hi David,

    Been trying a couple of things. Since it complained that it didn't finish downloading, I added a yield to "help" give it the time it needed. Like this (not entirely confident in my yield approach):

    Code portion

    WWW request = new WWW(url);

    yield return request;

    if(request.error == null)
    { ...

    The yield in itself seemed to be doing what I had hoped, and now the code logs the following warning


    Rejected because no crossdomain.xml policy file was found
    UnityEngine.Debug:LogWarning(Object)
    <LogScreen>c__Iterator0:MoveNext() (at Assets/Plugins/GoogleAnalytics/GoogleAnalytics.cs:68)

    Thank you again for this code and I'm very much looking forward to getting this up and running :)
     
    Last edited: Dec 4, 2013
  36. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    Great, let me know if it works out for you!

    It's been such a useful tool for us, glad you find it useful.
     
  37. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
    Hi David,

    Looks like I'm still in the dark a bit on how to get GA to respond. If you can see my post just above this one it hopefully details where I'm at enough and hopefully you have an idea for me to make an adjustment.

    Thank you very much.
     
  38. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    Have you tried testing on a device? Because of sandboxing and other oddities, you can have mixed results when testing on a computer and/or the Unity editor.
     
  39. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
    My application is currently running only on a computer via webplayer.
    I'll try with a build right now.

    Does that leave me out of this solution?
     
  40. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
    Hi David,

    No dice on GA responding to the webplayer version running on computer (or at least from what I'm seeing).

    I realize that your solution was based on mobile devices, however, my thought is that if send the info to GA wrapped in the URL that it would respond regardless of the origin of the sender. I'm sure my ignorance is showing here.

    Any other ideas or things to double check? I was pretty excited to see your solution as my web hosting defaults to GA for web stats and I love the idea of being able to track what level or scenes the player is using.

    Again, thank you.
     
  41. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    No worries.

    I can't say for certain if this will work for you or not. However - did you create your Google Analytics profile as a mobile app when you did the setup w/ them? That would definitely be required even if you just use webplayer.

    If all else fails, I do know that this plugin works with webplayer via Javascript injection: http://www.gameprefabs.com/products/show/200

    It's not as good for the use case that we have (hence why we rolled our own), but in your case it may be better.

    Thanks!
    David
     
  42. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
    Hi David,

    Yep, I've got to profiles setup one as a web and one as a mobile app each with their own property id, which I'm sure is the important difference, and I'm using the mobile app property id with the LogScreen function.

    Are you familiar with where I should put the crossdomain.xml? Perhaps where the web version of the game is downloaded?

    I'll take a look at the other suggestion you gave (thanks!).

    In the off chance you think of anything, please let me know.

    Thank you.
     
    Last edited: Dec 5, 2013
  43. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
    Hi David,

    On the off chance that I could get GA to respond I copied the URL string that was in LogScreen and just put it in a browser and sent it out and voila!

    GA is responding beautifully to sending just the URL in a browser. So, something that unity is doing on the way out is not preserving that ???

    Would you have any insight as to what the difference might be?

    Thank you (I'm getting closer!)
     
  44. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
  45. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
    Hi David,

    I ended up purchasing the GoogleUniversalAnalytics by Tonic on the Assets Store.

    I'm glad I attempted it myself first, cause I learned a lot and totally understand what is happening in the scripts.

    Thank you again for your contribution!

    Oh, also thanks Tonic!
     
  46. mrstupes

    mrstupes

    Joined:
    Nov 29, 2013
    Posts:
    8
    Hello, I feel almost embaressed to ask. Where exactly do you enter the 3 lines of code to implement the screen checking?
    I am new to programing, so this is a little over my head.
     
  47. Goaba Interactive

    Goaba Interactive

    Joined:
    Jan 4, 2014
    Posts:
    1
    Hi all,

    I've done a few changes to this plugin to include events, response handling and iPhone unique identifier.

    View attachment $GoogleAnalytics.cs

    I hope you find it useful.

    Thank you DavidJNels for the wonderful contribution!

    Cheers
     
    Krishx007 likes this.
  48. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    Great Goaba - thanks a lot for your contribution!
     
  49. heliorunner

    heliorunner

    Joined:
    Apr 27, 2013
    Posts:
    4
    Apple rejecting apps that access iOS device unique identifiers. This could be a problem...
     
  50. DavidJNels

    DavidJNels

    Joined:
    Aug 10, 2012
    Posts:
    34
    There's nothing in this plugin that uses UDIDs :)