gpt4 book ai didi

java - 使用 fragment 的 android 应用程序上的谷歌分析

转载 作者:行者123 更新时间:2023-11-30 01:25:34 26 4
gpt4 key购买 nike

我正在尝试将 Google Analytics 添加到我当前的应用程序中,但我不明白如何在我的应用程序代码中实现它。我想把它放在几个 fragment 上,下面是其中一个类。

我正在使用 Google 的指南:[ https://developers.google.com/analytics/devguides/collection/android/v4/#activity-or-fragment][1]而且我仍然不明白如何将它连接到我当前的页面。

这些是我的代码:

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);
}
}

我的应用程序.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;
}
}

分析.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>

谢谢你的帮助。汤姆

最佳答案

Google Analytics 的那些屏幕名称和事件不会出现在您的应用程序日志文件中。

你必须去Google Analytics website并使用您用于创建配置文件的电子邮件帐户登录。

登录后,您必须选择您的项目默认 View ,然后您将被重定向到您的应用概览页面。

并且该页面在左侧提供了菜单。您必须选择实时 --> 屏幕选项来观察当前的操作。

你所做的每一个 Action 只会在 3-5 分钟后反射(reflect)出来。

关于java - 使用 fragment 的 android 应用程序上的谷歌分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36471700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com