Here is my java file that defines my tracker:
package com.example.anantchowdhary.simpletodo;
import android.app.Application;
import android.content.pm.ApplicationInfo;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Logger;
import com.google.android.gms.analytics.Tracker;
public class MyApplication extends Application {
public Tracker mTracker;
public void startTracking()
{
if(mTracker==null)
{
GoogleAnalytics ga = GoogleAnalytics.getInstance(this);
mTracker = ga.newTracker(R.xml.track_app);
ga.enableAutoActivityReports(this);
mTracker.send(new HitBuilders.ScreenViewBuilder()
.setCustomMetric(1, 5)
.build()
);
}
}
public Tracker getTracker()
{
startTracking();
return mTracker;
}
}
MainActivity File:
package com.example.anantchowdhary.simpletodo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import com.example.anantchowdhary.simpletodo.R;
import java.util.ArrayList;
public class MainActivity extends Activity
{
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
items.add("First Item");
items.add("Second Item");
((MyApplication)getApplication()).getTracker();
}
public void onAddItem(View v) {
EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
items.add(etNewItem.getText().toString());
etNewItem.setText("");
}
}
Now I know that hits are transferred to Google Analytics, since I can see active users (1) in my Real Analytics Analytics toolbar on GA. And this is as soon as I enter the application.
However, my custom metric (with index 1) still displays 0.
It would be very helpful to help with this!
source
share