Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Google Analytics for Android LibGDX Game Screens

I´ve encountered a problem with implementing Google Analytics on a LibGDX Game App. I want to track the screens of the game, but Google Analytics works only in the android folder part of the Gradle project. In the core part, it doesn´t get the Context for the game screen classes, although I´ve added the libGoogleAnalyticsServices.jar to the core folder also. I´ve searched for solutions over the internet, but nothing worked out so I cannot initialize Google Analytics in the core part of the project.

This is a code snippet where I try to initialize Google Analytics:

GoogleAnalytics GA = GoogleAnalytics.getInstance();

and it gives me this error “ The type android.content.Context cannot be resolved. It is indirectly referenced from required .class files ”

Should I use the libGoogleAnalyticsServices.jar of the Android project in the Core also or should I use another jar file for this task?

Below are some links where I found information for this issue.

Google Event Tracker v3 https://developers.google.com/analytics/devguides/collection/android/v3/migration http://badlogicgames.com/forum/viewtopic.php?f=11&t=4298

Any answer on this specific issue would be very useful. Thank you in advance.

like image 551
Alex Lepar Avatar asked Feb 12 '23 09:02

Alex Lepar


1 Answers

You should use the Google Analytics sdk only in the android project, not in the core project. In the core project should only be platform-independent code. Don't import android libs into your core project.

If you want to track a screen view within core project you should can achieve that by using an interface. There is a wiki guide for interfacing libgdx

Your interface for tracking screen views could be something similar to this:

public interface Trackable {
   public void trackScreen(String screenName);
}

In your android project this method would then implement the Google Analytics tracking code.

like image 153
donfuxx Avatar answered Feb 14 '23 02:02

donfuxx