简体   繁体   中英

Google analytics not working with SDK V4

i was trying to integrate google analytics in my android app. and it didn't worked . i dont know where the problem is . here is the code

Main_activity:

    Tracker t = ((MyApplication) getApplication()).getTracker(TrackerName.APP_TRACKER);
    t.enableAdvertisingIdCollection(true);

global_tracker.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="ga_trackingId">UA-57284840-1</string>

    <integer name="ga_sessionTimeout">300</integer>

    <bool name="ga_autoActivityTracking">true</bool>

    <bool name="ga_reportUncaughtExceptions">true</bool>

    <screenName name="info.saidtagnit.hikamal3odama.QuotesActivity">Quotes_Activity</screenName>
    <screenName name="info.saidtagnit.hikamal3odama.AuthorsActivity">Authors_Activity</screenName>


</resources>

app_tracker.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" >

<!-- The apps Analytics Tracking Id -->
<string name="ga_trackingId">UA-57284840-1</string>

<!-- Percentage of events to include in reports -->
<string name="ga_sampleFrequency">100.0</string>

<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>

<!-- catch and report uncaught exceptions from the app -->
<bool name="ga_reportUncaughtExceptions">true</bool>

<!-- How long a session exists before giving up -->
<integer name="ga_sessionTimeout">-1</integer>

<!-- If ga_autoActivityTracking is enabled, an alternate screen name can be specified to
substitute for the full length canonical Activity name in screen view hit. In order to
specify an alternate screen name use an <screenName> element, with the name attribute
specifying the canonical name, and the value the alias to use instead. -->
    <screenName name="info.saidtagnit.hikamal3odama.QuotesActivity">Quotes_Activity</screenName>
    <screenName name="info.saidtagnit.hikamal3odama.AuthorsActivity">Authors_Activity</screenName>

</resources>

And here is MyApplication class:

package info.saidtagnit.hikamal3odama;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

import java.util.HashMap;

public class MyApplication extends Application {

// The following line should be changed to include the correct property id.
    public enum TrackerName {
        APP_TRACKER, // Tracker used only in this app.
        GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
      }

     HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

private static final String PROPERTY_ID = "UA-57284840-1";

//Logging TAG
private static final String TAG = "MyApp";

public static int GENERAL_TRACKER = 0;


public MyApplication() {
super();
}

synchronized Tracker getTracker(TrackerName trackerId) {
    if (!mTrackers.containsKey(trackerId)) {

      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID):analytics.newTracker(R.xml.global_tracker);
      mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
  }

}

You session timeout of -1 might be causing some issues.

Try removing the following line from your tracker definitions: -1

Also having two separate trackers that upload data to the same tracker id "UA-57284840-1" can cause duplicate uploads and double some number in your reports. I would suggest using single tracker for each tracker id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM