• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by helios · Jan 07, 2013 at 06:20 PM · unity analyticsjni

Possible to use Google Analytics with Android build?

Hello, I'm trying to set up Google Analytics with our Android build and am having a hard time getting it set up properly. I've downloaded the GoogleAnalytics.jar and placed it in Plugins/Android.

I'm trying to access functions contained in the .jar by using the JNI helper classes AndroidJavaClass and AndroidJavaHelper, but am having a little bit of trouble. How would I create a new Instance of a tracker?

What I'm trying to do using the helper classes to essentially do this:

 tracker = GoogleAnalyticsTracker.getInstance();
 tracker.start("UA-20274206-1", 10, this);

But I can't figure out the proper code on Unity's end. I've tried:

    AndroidJavaClass GATrackerClass = new AndroidJavaClass("com.google.android.apps.analytics.GoogleAnalyticsTracker"); 
    AndroidJavaObject tracker = GATrackerClass.CallStatic<AndroidJavaObject>("getInstance");

I get this error though:

 JNI: Unable to find method id for 'getInstance' (static)
 JNI: Init'd AndroidJavaObject with null ptr!

Any suggestions? Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mr.Z · Jan 07, 2013 at 10:33 PM

It most deffo is, not sure how tho, but you might want to check out this. Sorry I hope that helps.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image helios · Jan 08, 2013 at 03:03 PM 0
Share

Thanks, but I think that applies more for web tracking and not usage of Google's SD$$anonymous$$ within an Android build.

avatar image
1

Answer by DaveHawesEtx · Apr 20, 2013 at 04:01 PM

I have this part working at least

 AndroidJavaClass GoogleAnalyticsClass = new AndroidJavaClass("com.google.analytics.tracking.android.GoogleAnalytics");
 AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
             
             
            if(GoogleAnalyticsClass != null && cls_UnityPlayer != null)
            {
                 AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");    
                
                if(obj_Activity != null)
                {
                    m_gaInstance = GoogleAnalyticsClass.CallStatic<AndroidJavaObject>("getInstance",obj_Activity);
                }
            }    
             
             
            if(m_gaInstance != null)
            {
                m_gaInstance.Call("setDebug",true);
                
                m_tracker = m_gaInstance.Call<AndroidJavaObject>("getTracker","UA-XXXXXX-1");        
            }
    }

Which gets me a valid tracker. I did have a few issues before with getting sendEvent to work, but I have resolved those now, key thing was checking the jar file from google in eclipse and looking at the bytecode breakdown for the tracker class to make sure the method signature types matched exactly. In my case it was not matching the Long type, I was passing a long primitive data type, which has a method signature of J as opposed to the class java.lang.Long. So the send event call should look like.

 long lval = val;        
 AndroidJavaObject jo = new AndroidJavaObject("java.lang.Long", lval); 
 m_tracker.Call("sendEvent", category.ToString(), action,label,jo);

Where action and label are strings and val is an int.

Cheers,

Dave

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by oferei · Jul 18, 2013 at 03:57 PM

I got the Android SDK to work. I wrote a small plugin and connected it to Unity. Read about it in this post I wrote.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AndroidJNI reports lots of exceptions that don't propagate to Unity 0 Answers

Getting native Android String constants via JNI broken in Unity 2019.2.0? 3 Answers

Jar Conflict 1 Answer

Sending String of Data through JNI to Android Plugin 1 Answer

Running/Debuging a jar 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges