This document describes a legacy version of the Unity SDK. If your new to Analyitcs, use the latest SDK. Learn more

Google Analytics Plugin for Unity - Developer Guide

This document explains important concepts about using the Google Analytics Plugin for Unity.

Before you begin

Before you begin make sure you have completed the following items:

Setup and configuration

Choose a platform for configuration details:

Android
  1. If your project does not already have a file called AndroidManifest.xml in the <YOUR PROJECT ROOT>/Assets/Plugins/Android/ directory, build the project for Android and then copy the AndroidManifest.xml from the Temp/StagingArea/ directory (it will be under the directory you chose to build in). Paste it into <YOUR PROJECT ROOT>/Assets/Plugins/Android/.
  2. Add the following permissions to the AndroidManifest.xml in <YOUR PROJECT ROOT> /Assets/Plugins/Android/ above the <application> tag:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
          
  3. If you want to enable Campaign Tracking, add the service as follows:
    <service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
    <receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
              android:exported="true" >
      <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
      </intent-filter>
    </receiver>
          
iOS
  1. Add the following libraries to your Xcode project:
    • AdSupport.framework
    • CoreData.framework
    • SystemConfiguration.framework
    • libz.dylib
    • libsqlite3.dylib
  2. If you prefer to have the libraries automatically added when building for iOS, copy the file PostProcessBuildPlayer_GA from the iOS Extras directory and put it in the <YOUR PROJECT ROOT>/Assets/Editor directory.
  3. Download the mod_pbxproj.py script and copy it into the Editor directory. The libraries will now be added automatically during the build.

Configure the GAv4 prefab

To configure the GAv4 prefab:

  1. Click on the GAv4.prefab prefab object, which can be found in Assets/Plugins/GoogleAnalyticsV4 in the Project View. Drag this into the game hierarchy.
  2. The Inspector view will display a Script component attached to the prefab with several properties listed. Populate these properties with the correct values for your project. This will populate the object so that it can be used everywhere in your project. For example:
    • Android Tracking Code: UA-XXXXXXX-1
    • iOS Tracking Code: UA-XXXXXXX-2
    • Other Tracking Code: UA-XXXXXXX-3
    • Product Name: MyGame
    • Bundle Identifier: com.example.games
    • Bundle Version: 1.0
    • Dispatch Period: 5
    • Sample Frequency: 100
    • Log Level: VERBOSE
    • Anonymize IP: false
    • Uncaught Exception Reporting: false
    • Send Launch Event: false
    • Session Timeout: 1800
    • Dry Run: false
    • Advertiser Id Support - Send IDFA/AdID: false

Track an object

After configuring the GAv4 prefab, to track an object with Google Analytics:

  1. Identify a GameObject you wish to track (for example, a Player object) and click it in the Object Hierarchy view. If you do not already have a script attached to the object that you want to track then create a new script using the Add Component button in the Inspector view.
  2. Create a public variable in the class to hold the GoogleAnalyticsV4 object. For example:
    public GoogleAnalyticsV4 googleAnalytics;
  3. Save the script and return to the Unity window.
  4. In the Inspector view for the GameObject you wish to track, you will see the script you created and a field to populate called Google Analytics (or the variable name you chose). Drag the GAv4 object from the Object Hierarchy view onto the field.
  5. You are now ready to start tracking that GameObject. Repeat the process for other objects you wish to track using the same prefab.

Next steps

  • Review the API Reference to learn what kind of hits you can send to Google Analytics.
  • Read the Mobile App Implementation Guide to learn how to use Google Analytics to measure user interactions and answer questions about game usage.

Resources