简体   繁体   English

如何实现Google Analytics(分析)SDK Android V2

[英]How to implement google analytics sdk android v2

I want to implement google analytics sdk android v2 我想实施Google Analytics(分析)SDK Android V2

I had Update AndroidManifest.xml Create your analytics.xml file But i don't know hot to implement EasyTracker methods https://developers.google.com/analytics/devguides/collection/android/v2/#tracking-methods 我有Update AndroidManifest.xml创建您的analytics.xml文件,但是我不知道实现EasyTracker方法的热点https://developers.google.com/analytics/devguides/collection/android/v2/#tracking-methods

I don't know where to put it in my code? 我不知道将其放在代码中的什么位置? Sorry for my english... 对不起我的英语不好...

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.MailTo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;



    public class MainActivity extends Activity

     {

    private static final String MIME_TYPE_EMAIL = null;
    final Activity activity = this;

    @SuppressWarnings("deprecation")
    @Override


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setPluginsEnabled(true);
        webView.setWebChromeClient(new WebChromeClient()); 
        String scandinavianCharacters = null;
        webView.loadDataWithBaseURL(null, scandinavianCharacters, "text/html", "UTF-8", null);
        webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
        webView.setWebChromeClient(new WebChromeClient() {


            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Učitavanje...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                try {
                    webView.stopLoading();
                } catch (Exception e) {
                }
                try {
                    webView.clearView();
                } catch (Exception e) {
                }
                if (webView.canGoBack()) {
                    webView.goBack();
                }
                webView.loadUrl("file:///android_asset/greska/greska.html");
                super.onReceivedError(webView, errorCode, description, failingUrl);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl("http://mobile.mywebsite.com/test/");

    }
    public boolean shouldOverrideUrlLoading(WebView webview, String url)
    { 
        webview.loadUrl(url);
        return true;
    }


    WebView webView;
    @Override
    public void onBackPressed (){

        if (webView.isFocused() && webView.canGoBack()) {
                webView.goBack();       
        }
        else {
            new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Zatvorite aplikaciju?")
            .setMessage("Da li ste sigurni da želite da izađete?")
            .setPositiveButton("Da", new DialogInterface.OnClickListener()  {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish(); 
                    System.exit(0);
            }
        })
        .setNegativeButton("Ne", null)
        .show();
    }

        }

    public boolean shouldOverrideUrlLoading(webView view, String url) {
        if(url.startsWith("mailto:")){
            MailTo mt = MailTo.parse(url);
            Intent i = IntentSupport.newEmailIntent(MainActivity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
            startActivity(i);
            view.reload();
            return true;
        }

            else{
                view.loadUrl(url);
            }
            return true;
        }
   ;


public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
  intent.putExtra(Intent.EXTRA_TEXT, body);
  intent.putExtra(Intent.EXTRA_SUBJECT, subject);
  intent.putExtra(Intent.EXTRA_CC, cc);
  intent.setType(MIME_TYPE_EMAIL);
  return intent;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the currently selected menu XML resource.
    android.view.MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    return true;
}

/** Called when a menu item in the menu is clicked. */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
  case R.id.menuabout:
   Toast.makeText(this, " " +
        " " +
        "", Toast.LENGTH_LONG).show();
   return true;

  // Generic catch all for all the other menu resources
  default:
   if (!item.hasSubMenu())
       return true;

   break;
 }

 return false;
}

    }

You need to add two additional functions to your activity. 您需要向活动添加两个附加功能。 You can add them at the bottom of your class at the very end: 您可以在课程的最后添加它们:

@Override
public void onStart() {
   super.onStart();
   EasyTracker.getInstance().activityStart(this);
}

@Override
public void onStop() {
   super.onStop();
   EasyTracker.getInstance().activityStop(this);
}

The onStart() function is called after onCreate() so it will not affect the existing implementation. onStart()函数在onCreate()之后调用,因此不会影响现有的实现。 onStop() is called when the activity is stopped so it will catch things like your onBackPressed() action to stop the activity. 活动停止时会调用onStop(),因此它将捕获诸如onBackPressed()操作之类的内容来停止活动。

You can then choose to add additional tracking anywhere in your Activity using the send() functions such as the following: 然后,您可以选择使用send()函数在活动中的任何位置添加其他跟踪,例如:

EasyTracker.getTracker().sendEvent("Group", "Name", obj, value);

Unrelated by important - you should not be called System.exit(0) in your onBackPressed() function. 无关紧要-在onBackPressed()函数中不应将您称为System.exit(0)。 If this is the only activity in your application, calling finish() will be enough to exit the application. 如果这是应用程序中唯一的活动,则调用finish()足以退出该应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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