2013-10-04 105 views
2

我試圖將Google Analytics(分析)實現到我的Android應用程序中,但我遵循示例,它給了我createAppView無法解析或不是字段。我認爲這是因爲它沒有定義 - 但不應該在谷歌的例子中定義它?我不希望他們犯這樣的錯誤,我有一種感覺,我在我的最後做錯了事。createAppView無法解析或不是字段

要看到我使用的是有在「完整的例子,」在這裏看一下例子:

https://developers.google.com/analytics/devguides/collection/android/v3/advanced

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.Build; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.provider.Settings; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.res.AssetManager; 
import android.graphics.drawable.AnimationDrawable; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import com.google.analytics.tracking.android.Fields; 
import com.google.analytics.tracking.android.GAServiceManager; 
import com.google.analytics.tracking.android.GoogleAnalytics; 
import com.google.analytics.tracking.android.Logger.LogLevel; 
import com.google.analytics.tracking.android.MapBuilder; 
import com.google.analytics.tracking.android.Tracker; 

import android.app.Application; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 

@SuppressWarnings("unused") 
public class StartActivity extends Activity { 

    private AnimationDrawable mGoButtonAnimation; 
    Context c; 
    boolean isAirPlaneMode; 
    int simState; 
    TelephonyManager tm; 
    boolean NetworkConnection = false; 
    AlertDialog mConfirmAlert = null; 
    private static GoogleAnalytics mGa; 
    private static Tracker mTracker; 
    private static final String GA_LABEL = "Google Analytics"; 
    /* 
    * Google Analytics configuration values. 
    */ 
    // Placeholder property ID. 
    private static final String GA_PROPERTY_ID = "UA-XXXX-Y"; 

    // Dispatch period in seconds. 
    private static final int GA_DISPATCH_PERIOD = 30; 

    // Prevent hits from being sent to reports, i.e. during testing. 
    private static final boolean GA_IS_DRY_RUN = false; 

    // GA Logger verbosity. 
    private static final LogLevel GA_LOG_VERBOSITY = LogLevel.INFO; 

    // Key used to store a user's tracking preferences in SharedPreferences. 
    private static final String TRACKING_PREF_KEY = "trackingPreference"; 

    /* 
    * Method to handle basic Google Analytics initialization. This call will 
    * not block as all Google Analytics work occurs off the main thread. 
    */ 
    private void initializeGa() { 
     mGa = GoogleAnalytics.getInstance(this); 
     mTracker = mGa.getTracker(GA_PROPERTY_ID); 

     // Set dispatch period. 
     GAServiceManager.getInstance().setLocalDispatchPeriod(
       GA_DISPATCH_PERIOD); 

     // Set dryRun flag. 
     mGa.setDryRun(GA_IS_DRY_RUN); 

     // Set Logger verbosity. 
     mGa.getLogger().setLogLevel(GA_LOG_VERBOSITY); 

     // Set the opt out flag when user updates a tracking preference. 
     SharedPreferences userPrefs = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     userPrefs 
     .registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() { 
      @Override 
      public void onSharedPreferenceChanged(
        SharedPreferences sharedPreferences, String key) { 
       if (key.equals(TRACKING_PREF_KEY)) { 
        GoogleAnalytics 
        .getInstance(getApplicationContext()) 
        .setAppOptOut(
          sharedPreferences.getBoolean(key, 
            false)); 
       } 
      } 
     }); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.start); 
     initializeGa(); 
     StartActivity.getGaTracker().set(Fields.SCREEN_NAME, GA_LABEL); 
     tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     simState = tm.getSimState(); 
     System.out.println("Sim State" + simState); 
     Button goButton = (Button) findViewById(R.id.go_button); 
     // Set GO button to drawable animation 
     goButton.setBackgroundResource(R.drawable.go_button_animation); 
     mGoButtonAnimation = (AnimationDrawable) goButton.getBackground(); 

     // check network availability 
     NetworkConnection = CheckNetworkAvailability 
       .CheckNetworkAvailability(StartActivity.this); 
     if (!NetworkConnection) { 

      showAlert("Network Connection is not Available"); 
     } 
     isAirPlaneMode = isAirplaneModeOn(StartActivity.this); 
     goButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Start UpdateActivity 

       if (simState == TelephonyManager.SIM_STATE_ABSENT) { 
        showAlert("Sim Card is absent, Please insert a Net10 Sim Card"); 

       } else if (isAirPlaneMode != false) { 
        showAlert("Please Insert a Net10 Sim Card or Turn on the AirPlane Mode and Re-Run the app"); 

       } else if (simState == TelephonyManager.SIM_STATE_NETWORK_LOCKED 
         || simState == TelephonyManager.SIM_STATE_PIN_REQUIRED 
         || simState == TelephonyManager.SIM_STATE_PUK_REQUIRED 
         || simState == TelephonyManager.SIM_STATE_UNKNOWN) { 
        showAlert("Sim Card is absent, Please insert a Net10 Sim Card"); 
       } else if (simState == TelephonyManager.SIM_STATE_READY) { 
        Intent i = new Intent(StartActivity.this, 
          UpdateActivity.class); 
        startActivity(i); 
        finish(); 
       } 

      } 

     }); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // Send a screen view when the Activity is displayed to the user. 
     StartActivity.getGaTracker().send(MapBuilder.createAppView.build()); 
    } 

    /* 
    * Returns the Google Analytics tracker. 
    */ 
    public static Tracker getGaTracker() { 
     return mTracker; 
    } 

    /* 
    * Returns the Google Analytics instance. 
    */ 
    public static GoogleAnalytics getGaInstance() { 
     return mGa; 
    } 

    /** 
    * * Gets the state of Airplane Mode. * * @param context * @return true if 
    * enabled. 
    */ 
    public static boolean isAirplaneModeOn(Context context) { 

     return Settings.System.getInt(context.getContentResolver(), 
       Settings.System.AIRPLANE_MODE_ON, 0) != 0; 

    } 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     // Animate GO button when corresponding window is in focus 
     mGoButtonAnimation.start(); 
    } 

    private void showAlert(String message) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage(message).setPositiveButton("OK", 
       new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       StartActivity.this.finish(); 
      } 
     }); 
     mConfirmAlert = builder.create(); 
     mConfirmAlert.show(); 

    } 

} 

回答

5

這是example provided by Google中的錯字。其實,createAppView是一種方法,而不是變量,那麼:

StartActivity.getGaTracker().send(MapBuilder.createAppView.build()); 

應該是:

StartActivity.getGaTracker().send(MapBuilder.createAppView().build()); 
+0

問題是MapBuilder甚至沒有擺在首位存在!即使我已經使用了「import com.google.analytics.tracking.android。*;',似乎Google的文檔已經過時了v3分析庫,MapBuilder無法解析。 –

+0

感謝你們倆 –

2

相反的MapBuilder.createAppView,它應該是HitBuilders.ScreenViewBuilder()在谷歌Analytics(分析)API v4的

更換

StartActivity.getGaTracker().send(MapBuilder.createAppView().build());

StartActivity.getGaTracker().send(HitBuilders.ScreenViewBuilder().build());