Search code examples
javaandroidgoogle-analytics-firebase

Why a screen view is sent twice to Google Analytics on android


I am using Google Analytics for Android v4. I have attached code, whats happening is each time i dispatch the local hit , it is sent twice and appears twice on Dashboard once for Screen name and once for Activity package name, not sure whats wrong with code.

The global_tracker.xml file

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

        <integer name="ga_sessionTimeout">300</integer>
        <bool name="ga_autoActivityTracking">true</bool>
        <bool name="ga_anonymizeIp">true</bool>
        <string name="ga_trackingId">UA-XXXXXXX-2</string>
        <int name="ga_dispatchPeriod">-10</int>
    </resources>

The activity code

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        webFunction = new WebFunctions();
        dbFunction = new DatabaseFuncitons();
        progressDialog = new ProgressDialog(ActivityLogin.this);

        editTextUsername = (EditText) findViewById(R.id.editTextUsername);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);

        Tracker t = GoogleAnalytics.getInstance(this).newTracker(R.xml.global_tracker);
        t.setScreenName("/Login");

        t.send(new HitBuilders.ScreenViewBuilder().build());

        GoogleAnalytics.getInstance(getBaseContext()).dispatchLocalHits();

        GoogleAnalytics.getInstance(this).getLogger()
                .setLogLevel(Logger.LogLevel.VERBOSE);
    }

Solution

  • The code is doing exactly what you told it to do.

    Here, you send out a hit:

    t.send(new HitBuilders.ScreenViewBuilder().build());
    

    then on the next line, you manually dispatch local hits:

    GoogleAnalytics.getInstance(getBaseContext()).dispatchLocalHits();