我的应用中有很多活动。我正在为xml文件中的所有活动设置屏幕名称。但是在谷歌分析活动屏幕名称中没有显示
我的代码在下面有什么问题请帮帮我一些
public final class AnalyticsTrackers {
public enum Target {
APP,
// Add more trackers here if you need, and update the code in #get(Target) below
}
private static AnalyticsTrackers sInstance;
public static synchronized void initialize(Context context) {
if (sInstance != null) {
throw new IllegalStateException("Extra call to initialize analytics trackers");
}
sInstance = new AnalyticsTrackers(context);
}
public static synchronized AnalyticsTrackers getInstance() {
if (sInstance == null) {
throw new IllegalStateException("Call initialize() before getInstance()");
}
return sInstance;
}
private final Map<Target, Tracker> mTrackers = new HashMap<Target, Tracker>();
private final Context mContext;
/**
* Don't instantiate directly - use {@link #getInstance()} instead.
*/
private AnalyticsTrackers(Context context) {
mContext = context.getApplicationContext();
}
public synchronized Tracker get(Target target) {
if (!mTrackers.containsKey(target)) {
Tracker tracker;
switch (target) {
case APP:
tracker = GoogleAnalytics.getInstance(mContext).newTracker(R.xml.app_tracker);
break;
default:
throw new IllegalArgumentException("Unhandled analytics target " + target);
}
mTrackers.put(target, tracker);
}
return mTrackers.get(target);
}
}
public class MyApplication extends Application {
public static final String TAG = MyApplication.class
.getSimpleName();
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
AnalyticsTrackers.initialize(this);
AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP);
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
public synchronized Tracker getGoogleAnalyticsTracker() {
AnalyticsTrackers analyticsTrackers = AnalyticsTrackers.getInstance();
return analyticsTrackers.get(AnalyticsTrackers.Target.APP);
}
/***
* Tracking screen view
*
* @param screenName screen name to be displayed on GA dashboard
*/
public void trackScreenView(String screenName) {
Tracker t = getGoogleAnalyticsTracker();
// Set screen name.
t.setScreenName(screenName);
// Send a screen view.
t.send(new HitBuilders.ScreenViewBuilder().build());
GoogleAnalytics.getInstance(this).dispatchLocalHits();
}
/***
* Tracking exception
*
* @param e exception to be tracked
*/
public void trackException(Exception e) {
if (e != null) {
Tracker t = getGoogleAnalyticsTracker();
t.send(new HitBuilders.ExceptionBuilder()
.setDescription(
new StandardExceptionParser(this, null)
.getDescription(Thread.currentThread().getName(), e))
.setFatal(false)
.build()
);
}
}
/***
* Tracking event
*
* @param category event category
* @param action action of the event
* @param label label
*/
public void trackEvent(String category, String action, String label) {
Tracker t = getGoogleAnalyticsTracker();
// Build and send an Event.
t.send(new HitBuilders.EventBuilder().setCategory(category).setAction(action).setLabel(label).build());
}
}
public class MainActivity extends AppCompatActivity {
private static String TAG = MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private Button btnSecondScreen, btnSendEvent, btnException, btnAppCrash, btnLoadFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
btnSecondScreen = (Button) findViewById(R.id.btnSecondScreen);
btnSendEvent = (Button) findViewById(R.id.btnSendEvent);
btnException = (Button) findViewById(R.id.btnException);
btnAppCrash = (Button) findViewById(R.id.btnAppCrash);
btnLoadFragment = (Button) findViewById(R.id.btnLoadFragment);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
/**
* Launching another activity to track the other screen
*/
btnSecondScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
/**
* Event tracking
* Event(Category, Action, Label)
*/
btnSendEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Tracking Event
MyApplication.getInstance().trackEvent("Book", "Download", "Send event example");
Toast.makeText(getApplicationContext(), "Event \'Book\' \'Download\' \'Event example\' is sent. Check it on Google Analytics Dashboard!", Toast.LENGTH_LONG).show();
}
});
/**
* Tracking Exception Manually
* All known exceptions can be tracking this way
* using Try & Catch
*/
btnException.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String name = null;
if (name.equals("ravi")) {
/* Never comes here as it throws null pointer exception */
}
} catch (Exception e) {
// Tracking exception
MyApplication.getInstance().trackException(e);
Toast.makeText(getApplicationContext(), getString(R.string.toast_track_exception), Toast.LENGTH_LONG).show();
Log.e(TAG, "Exception: " + e.getMessage());
}
}
});
/**
* Tracking App Crashes
* Manually generation app crash by dividing with zero
*/
btnAppCrash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), getString(R.string.toast_app_crash), Toast.LENGTH_LONG).show();
Runnable r = new Runnable() {
@Override
public void run() {
int answer = 12 / 0;
}
};
Handler h = new Handler();
h.postDelayed(r, 1500);
}
});
/**
* Tracking Fragment View
*/
btnLoadFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FooterFragment footerFragment = new FooterFragment();
fragmentTransaction.replace(R.id.frame_container, footerFragment);
fragmentTransaction.commit();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- End current session if app sleeps for a period of time -->
<integer name="ga_sessionTimeout">300</integer>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The property id associated with this analytics tracker -->
<string name="ga_trackingId">UA-79508091-1</string>
<string name="ga_sampleFrequency">100.0</string>
<bool name="ga_reportUncaughtExceptions">true</bool>
<screenName name="info.androidhive.googleanalytics.activity.MainActivity">Home Screen</screenName>
<screenName name="info.androidhive.googleanalytics.activity.SecondActivity">Second Screen</screenName>
<screenName name="info.androidhive.googleanalytics.activity.FooterFragment">Footer Fragment</screenName>
<!--
See Project Structure -> Analytics -> Google Analytics -> Learn More
to learn more about configuring this file.
-->
</resources>
答案 0 :(得分:0)
只要&#39; ga_autoActivityTracking &#39;就会使用上面描述的XML配置(app_tracker.xml)。设置为true。这意味着只要活动开始,跟踪就会自动报告新的屏幕视图。 这意味着,如果您手动配置/发送屏幕名称不会出现在报告中。所以你必须将 ga_autoActivityTracking 设置为 false 。
<!-- off automatic Activity measurement -->
<bool name="ga_autoActivityTracking">false</bool>