0

I want to implement 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

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

    }
4

1 回答 1

0

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

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

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

onStart() 函数在 onCreate() 之后调用,因此它不会影响现有的实现。onStop() 在活动停止时被调用,所以它会捕捉到你的 onBackPressed() 动作来停止活动。

然后,您可以选择使用 send() 函数在 Activity 的任何位置添加额外的跟踪,如下所示:

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

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

于 2013-07-07T18:15:42.600 回答