1

谷歌有一个关于移动设备分析数据的例子。此处提供了代码。我添加了 jar 文件,一切编译正常,但运行应用程序时出错。出于安全原因,我取出了 UA ID(不确定这是否重要)

LogCat 为我提供了以下信息:E/AndroidRuntime(1175): java.lang.NoClassDefFoundError: com.google.android.apps.analytics.GoogleAnalyticsTracker AND E/AndroidRuntime(1130):at 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();
    }
}
4

2 回答 2

9

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

右键单击您的项目 -> Build Path-> Configure Build Path-> 选择Order and Export选项卡 -> 检查GoogleAnalyticsJar.jar-> 按OK

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

于 2012-07-02T18:07:22.303 回答
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

于 2014-08-22T11:48:03.750 回答