Integrate Google Analytics and Admob into Unity3D

Recently I’ve been focusing on mobile game development using Unity3D and fount it necessary to add some plugins to monitor user actions and monetize. After searching on the web for some time, I decided to utilize Google Analytics and Google Admob. However it did take me some time to integrate them into Unity3D project. So I’m simply listing all the things I have done in case this might be helpful to you.

Integrate Google Admob into Unity3D project for iOS

1. Register on Google Admob and get a publisher ID.
2. Download the latest Admob plugin for Unity3D on Google Developer’s Website .
3. Import Admob plugin to your Unity3D project.
4. Add C# code for showing ads. Remember to modify publisherID in the code.
5. Build an Xcode project from your Unity3D project
6. Download the latest Google Admob SDK for iOS from Admob’s Official Site.
7. Follow the instructions listed in the ReadMe file to be able to compile :
7.1 Add SDK files into the Xcode project
7.2 Add “-ObjC” flag in Other Linker Flags in Build Settings
7.3 Import libraries and frameworks needed by SDK, including: AdSupport, AudioToolbox, AVFoundation, CoreGraphics, CoreTelephony, MessageUI, StoreKit, SystemConfiguration
8. Build the project and test your app out on your device.

Actually some weird problems occurred while finishing these steps…

Problem #1 :
When building the project, the linker outputs “library not found for -liphone-lib”, which terminates the building process.
Reason :
It seems that when adding files from the Admob SDK into Xcode project, the Library Search Paths param in Build Settings is not modified in the right way.
Solution :
Modify Library Search Paths param in Build Settings, delete the \” characters in the beginning and ending position so it is exactly  $(SRCROOT)/Libraries

Problem #2:
My code called the method AdMobPlugin.CreateBannerView and set the param positionAtTop to false, hoping the ad banner will show at the bottom of the screen. To my disappointment, that param is not working at all. The ad will always be showing at top.
Reason :
The implementation for CreateBannerView is not complete in the SDK. The flag is not working.
Solution :
Open AdMobPlugin.mm file in the Xcode project and modify the createGADBannerViewWithPubId method like this :

 - (void)createGADBannerViewWithPubId:(NSString *)pubId bannerType:(GADAdSize)adSize
{
    self.bannerView = [[[GADBannerView alloc] initWithAdSize:adSize] autorelease];
    if (!positionAdAtTop_)
    {
        [self.bannerView setFrame:CGRectMake(self.bannerView.bounds.origin.x,

                                             UnityGetGLViewController().view.frame.size.height - self.bannerView.bounds.size.height,

                                             self.bannerView.bounds.size.width,

                                             self.bannerView.bounds.size.height)];

    }
    self.bannerView.adUnitID = pubId;
    self.bannerView.delegate = self;
    self.bannerView.rootViewController = UnityGetGLViewController();
}

Everything is working properly now :-)
Still another thing to note, the AdBannerView won’t get destroyed when we create a new one. It will definitely cause memory leak.

Integrate Google Analytics into Unity3D project

Google Analytics is a very handy toolset for indie game developers. It is easy to set up and totally free. Thanks to Mass Threat Labs, we can finish GA integration in minutes.
1. Set up your Google Analytics account.
2. Download the plugin on Mass Threat Labs’s website.
3. Import the plugin into your Unity project.
4. Drag the prefab into your first Scene.
5. Call the API anywhere you want to report.

The real-time graph is super cool =]

  1. I am trying to figure out how to call the Analytics in UnityScript but C# is cool too. It seems tricky.