Reputation:
I am trying to add Google Analytics to my current app but I can't understand how to implement it in my app code. I would like to put it on several fragments, below is one of the classes.
I am using Google's guide : [https://developers.google.com/analytics/devguides/collection/android/v4/#activity-or-fragment][1] And I still don't understand how to connect it to my current pages.
These are my codes :
LoginActivity2.java:
package com.example.tomgoldberg.tasker;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
/**
* Created by TomGoldberg on 20.3.16.
*/
public class LoginActivity2 extends Fragment implements View.OnClickListener , View.OnFocusChangeListener {
static FragmentManager fragmentManager;
private static final String TAG = "LoginActivity2";
View myView;
String email , password;
EditText _emailTextView;
EditText _passwordTextView;
private Button _logInView;
private Button _signup;
boolean verEmail = false;
boolean verPass = false;
boolean insert = false;
private Tracker mTracker;
//static DatabaseHelper _myDB;
FragmentActivity faActivity ;
public LoginActivity2() {
//Functions.initializeData_forPendingTasks();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.activity_login, container, false);
fragmentManager = getFragmentManager();
MyApplication application = (MyApplication) faActivity.getApplication();
mTracker = application.getDefaultTracker();
//Functions._drawerH1 = (TextView)myView.findViewById(R.id.navigationDrawer_h1);
//Functions._drawerH2 = (TextView)myView.findViewById(R.id.navigationDrawer_h2);
_logInView = (Button)myView.findViewById(R.id.loginBottun);
_logInView.setOnClickListener(this);
_signup = (Button)myView.findViewById(R.id.signUp_button);
_signup.setOnClickListener(this);
_emailTextView = (EditText)myView.findViewById(R.id.email_login_text);
//_emailTextView.setOnFocusChangeListener(this);
_passwordTextView = (EditText)myView.findViewById(R.id.password_login_text);
//_passwordTextView.setOnFocusChangeListener(this);
faActivity = getActivity();
Functions._myDB = new DatabaseHelper(faActivity);
Functions.insert();
Functions.initializeData_forAllTasksFragment();
Functions.initializeData_forPendingTasks();
Functions.initializeData_forPriorityTasks();
Functions.initializeData_forCategoryTasks();
return myView;
}
@Override
public void onClick(View v) {
if ( v.getId() == _logInView.getId() ){
email = _emailTextView.getText().toString();
password = _passwordTextView.getText().toString();
try{
if( Functions.checkingManager(email,password) ){
Snackbar.make(myView.findViewById(R.id.Login1mainLayout), "Welcome back manager " + Functions._userName , Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
try{
NavigationDrawer._drawerH1.setText(Functions._userName);
NavigationDrawer._drawerH2.setText(Functions._userEmail);
} catch (Exception E){
System.out.println("Unable to change...--> "+E.getMessage());
}
Fragment fragment = new MainMenu();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack("stack_MainMenu_after_LOGIN");
transaction.commit();
}
else if ( Functions.checkingMember(email, password)){
Snackbar.make(myView.findViewById(R.id.Login1mainLayout), "Welcome back member " + Functions._userName , Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
try{
NavigationDrawer._drawerH1.setText(Functions._userName);
NavigationDrawer._drawerH2.setText(Functions._userEmail);
} catch (Exception E){
System.out.println("Unable to change...--> "+E.getMessage());
}
Fragment fragment = new MainMenu();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack("stack_MainMenu_after_LOGIN");
transaction.commit();
}
else {
_logInView.setError("Some error");
Snackbar.make(myView.findViewById(R.id.Login1mainLayout), "Please provide your Email and Password", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
_logInView.setError(null);
}
} catch (Exception e){
_logInView.setError("Some error");
Snackbar.make(myView.findViewById(R.id.Login1mainLayout), "Please provide your Email and Password", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
_logInView.setError(null);
}
}
else if ( v.getId() == _signup.getId() ){
Fragment fragment = new SignUpAsManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack("stack_SignUp");
transaction.commit();
}
}
@Override
public void onFocusChange( View v , boolean hasFocus ) {
if ( v.getId() == _emailTextView.getId() && !hasFocus ) {
verifyEmail();
_emailTextView = (EditText)myView.findViewById(R.id.email_login_text);
}
if (v.getId() == _passwordTextView.getId() && !hasFocus) {
verifyPassword();
_passwordTextView = (EditText) myView.findViewById(R.id.password_login_text);
}
}
public void verifyEmail(){
String emailToCheck = _emailTextView.getText().toString();
if ( TextUtils.isEmpty(emailToCheck) || Functions.hasNoLetters(emailToCheck) || Functions.isEmail(emailToCheck)) {
_emailTextView.setError("Please Provide Your Email Address");
verEmail = false;
}
else if( TextUtils.isDigitsOnly(emailToCheck) ) {
_emailTextView.setError("Email Address Only");
verEmail = false;
}
else {
System.out.println("verifyEmail");
try{
verEmail = true ;
_logInView.setError(null);
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
public void verifyPassword(){
String passwordToCheck = _passwordTextView.getText().toString();
if (TextUtils.isEmpty(passwordToCheck)||Functions.hasNoLetters(passwordToCheck)){
_passwordTextView.setError("Please Provide Your Password");
System.out.println("verifyPassword");
verPass = false;
}
else {
try{
verPass = true;
_logInView.setError(null);
}
catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("verifyPassword");
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Log.i(TAG, "Setting screen name: " + name);
mTracker.setScreenName("Image~" + name);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
mTracker.send(new HitBuilders.EventBuilder()
.setCategory("Action")
.setAction("Share")
.build());
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyApplication.java:
package com.example.tomgoldberg.tasker;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
/**
* Created by tommyahav on 07/04/2016.
*/
public class MyApplication extends Application {
private Tracker mTracker;
synchronized public Tracker getDefaultTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
mTracker = analytics.newTracker(R.xml.global_tracker);
}
return mTracker;
}
}
Analytics.xml :
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
<!--Replace placeholder ID with your tracking ID-->
<string name="ga_trackingId">UA-75923329-1</string>
<!--Enable automatic activity tracking-->
<bool name="ga_autoActivityTracking">true</bool>
<!--Disable automatic exception tracking-->
<bool name="ga_reportUncaughtExceptions">false</bool>
</resources>
Thank you for helping. Tom
Upvotes: 1
Views: 419
Reputation: 755
Those screen names and events of the google analytics won't appear in your application log file.
You have to go Google Analytics website and log in with your email account which you used to create the configuration file.
After login you have to choose your project Default view and then you will be redirected to your app overview page.
And that page provided with the menus on the left side. you have to select Real Time --> Screens option to watch out the current actions.
Every action you made will be reflected after 3-5 minutes only.
Upvotes: 1