Google Analytic Android示例

时间:2012-07-02 17:45:00

标签: java android google-analytics google-analytics-api

Google有一个关于移动设备分析数据的示例。代码在这里提供。我添加了jar文件,所有内容编译都很好,但是运行应用程序时出错了。 出于安全原因,我拿出了UA ID(不确定是否重要)

LogCat为我提供了以下信息: E / AndroidRuntime(1175):java.lang.NoClassDefFoundError:com.google.android.apps.analytics.GoogleAnalyticsTracker 的 E / AndroidRuntime(1130):在com.google.android.apps.analytics.sample.TestActivity.onCreate(TestActivity.java:19)

从它的内容来看,它无法找到googleAnalyticTracker?但如果它编好了,为什么不能找到呢?

package com.google.android.apps.analytics.sample;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestActivity extends Activity {

    GoogleAnalyticsTracker tracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tracker = GoogleAnalyticsTracker.getInstance();

        // Start the tracker in manual dispatch mode...
        // tracker.startNewSession("", this);

        // ...alternatively, the tracker can be started with a dispatch interval
        // (in seconds).
        tracker.startNewSession("", 20, this);

        setContentView(R.layout.main);
        Button createEventButton = (Button) findViewById(R.id.NewEventButton);
        createEventButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tracker.trackEvent("Clicks", // Category
                        "Button", // Action
                        "clicked", // Label
                        77); // Value
            }
        });

        Button createPageButton = (Button) findViewById(R.id.NewPageButton);
        createPageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Add a Custom Variable to this pageview, with name of "Medium"
                // and value "MobileApp"
                tracker.setCustomVar(1, "Medium", "Mobile App");
                // Track a page view. This is probably the best way to track
                // which parts of your application
                // are being used.
                // E.g.
                // tracker.trackPageView("/help"); to track someone looking at
                // the help screen.
                // tracker.trackPageView("/level2"); to track someone reaching
                // level 2 in a game.
                // tracker.trackPageView("/uploadScreen"); to track someone
                // using an upload screen.
                tracker.trackPageView("/testApplicationHomeScreen");
            }
        });

        Button quitButton = (Button) findViewById(R.id.QuitButton);
        quitButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Button dispatchButton = (Button) findViewById(R.id.DispatchButton);
        dispatchButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Manually start a dispatch, not needed if the tracker was
                // started with a dispatch
                // interval.
                tracker.dispatch();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Stop the tracker when it is no longer needed.
        tracker.stopSession();
    }
}

2 个答案:

答案 0 :(得分:9)

我更新了我的Android SDK后也遇到了同样的问题。我通过执行以下操作解决了这个问题:

右键点击您的项目 - > Build Path - > Configure Build Path - >选择Order and Export标签 - >查看GoogleAnalyticsJar.jar - >按OK

这有助于我解决问题。希望它也能帮助你。

答案 1 :(得分:0)

在布局文件夹中创建alalytics.xml并粘贴以下代码。

  <?xml version="1.0" encoding="utf-8" ?>
    <resources>

      <!--Replace placeholder ID with your tracking ID-->

      <string name="ga_trackingId">UA-XXXX-Y</string>

      <!--Enable automatic activity tracking-->

      <bool name="ga_autoActivityTracking">true</bool>

      <!--Enable automatic exception tracking-->

      <bool name="ga_reportUncaughtExceptions">true</bool>

    </resources>

Create Helper.java class and paste the following code.

import android.content.Context;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.google.tagmanager.DataLayer;
import com.google.tagmanager.TagManager;
public class Helper{

                      /**
                       * Push an "openScreen" event with the given screen name. Tags that match that event will fire.
                       */
                     private static Tracker tracker;
                      public static void pushOpenScreenEvent(Context context, String screenName) {

                          // Instantiate the Tracker
                                           tracker =  EasyTracker.getInstance(context);
                                           tracker.set(Fields.SCREEN_NAME, screenName);
                                         // Send a screenview.
                                           tracker.send(MapBuilder
                                             .createAppView()
                                             .build()
                                           );
                      }

                      /**
                       * Push an "Button clicked" event with the given screen name. Tags that match that event will fire.
                       */
                      public static void pushbtnClickedEvent(Context context, String clickE) {
                                           tracker =  EasyTracker.getInstance(context);
                                         // Values set directly on a tracker apply to all subsequent hits.
                                           tracker.set(Fields.SCREEN_NAME, "Home Screen");

                                           // This screenview hit will include the screen name "Home Screen".
                                           tracker.send(MapBuilder.createAppView().build());

                                           // And so will this event hit.
                                           tracker.send(MapBuilder
                                             .createEvent("UI", "click", "my btn clicked", null)
                                             .build()
                                           );
                      }
                      /**
                       * Push a "closeScreen" event with the given screen name. Tags that match that event will fire.
                       */
                      public static void pushCloseScreenEvent(Context context, String screenName) {

                          // Instantiate the Tracker
                                           tracker =  EasyTracker.getInstance(context);
                                           tracker.set(Fields.SCREEN_NAME, screenName);
                                         // Send a screenview.
                                           tracker.send(MapBuilder
                                             .createAppView()
                                             .build()
                                           );
                      }
}

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.analytics.tracking.android.EasyTracker;

public class MainActivity extends Activity {


 Button btnClickEvent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnClickEvent=(Button)findViewById(R.id.button1);
        btnClickEvent.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub

    Helper.pushbtnClickedEvent(MainActivity.this, "Button Clicked");

   }
  });
    }


    @Override
    protected void onStart() {
       super.onStart();
       EasyTracker.getInstance(this).activityStart(this);
       Helper.pushOpenScreenEvent(this, "Main Activity");

    }
 @Override
 protected void onStop() {
                      // TODO Auto-generated method stub

                      super.onStop();
                       EasyTracker.getInstance(this).activityStop(this); // Add this method




 } 
}

现在,您的应用程序已准备就绪,只需按“按钮”,然后打开您的Google分析仪表板。

不要忘记在manifest.xml中添加这些权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

参考:http://velmuruganandroidcoding.blogspot.in/2014/08/google-analytics-in-android.html