如何实施谷歌分析sdk android v2

时间:2013-07-07 17:59:10

标签: android eclipse google-analytics

我想实施google analytics sdk android v2

我有 更新AndroidManifest.xml 创建analytics.xml文件 但我不知道如何实施EasyTracker方法 https://developers.google.com/analytics/devguides/collection/android/v2/#tracking-methods

我不知道在我的代码中把它放在哪里? 对不起我的英文......

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

    }

1 个答案:

答案 0 :(得分:0)

您需要为活动添加两个附加功能。你可以在最后的课程底部添加它们:

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

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

在onCreate()之后调用onStart()函数,因此它不会影响现有的实现。当活动停止时调用onStop(),因此它将捕获onBackPressed()操作以停止活动。

然后,您可以选择使用send()函数在Activity中的任何位置添加其他跟踪:

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

不重要 - 您的onBackPressed()函数不应该调用System.exit(0)。如果这是应用程序中唯一的活动,则调用finish()将足以退出应用程序。