Skip to content
This repository
Browse code

Added Google Analytics support.

  • Loading branch information...
commit a7ada65a201bcab3f0d1a74fe4a517d70d31278f 1 parent 72c760a
Carlos Yaconi H. cyaconi authored
BIN  libs/libGoogleAnalytics.jar
Binary file not shown
4 src/com/prey/FileConfigReader.java
@@ -77,5 +77,9 @@ public boolean isAskForPassword() {
77 77 public boolean isLogEnabled() {
78 78 return Boolean.parseBoolean(properties.getProperty("log-enabled"));
79 79 }
  80 +
  81 + public String getAnalyticsUA() {
  82 + return properties.getProperty("analytics-ua");
  83 + }
80 84
81 85 }
6 src/com/prey/activities/AddDeviceToAccountActivity.java
@@ -22,6 +22,7 @@
22 22 import android.widget.EditText;
23 23 import android.widget.Toast;
24 24
  25 +import com.google.android.apps.analytics.GoogleAnalyticsTracker;
25 26 import com.prey.PreyAccountData;
26 27 import com.prey.R;
27 28 import com.prey.exceptions.NoMoreDevicesAllowedException;
@@ -139,6 +140,11 @@ protected void onPostExecute(Void unused) {
139 140 String message = getString(R.string.device_added_congratulations_text);
140 141 Bundle bundle = new Bundle();
141 142 bundle.putString("message", message);
  143 + GoogleAnalyticsTracker.getInstance().trackEvent(
  144 + "Device", // Category
  145 + "Added", // Action
  146 + "", // Label
  147 + 1);
142 148 Intent intent = new Intent(AddDeviceToAccountActivity.this, CongratulationsActivity.class);
143 149 intent.putExtras(bundle);
144 150 startActivity(intent);
7 src/com/prey/activities/CreateAccountActivity.java
@@ -7,6 +7,8 @@
7 7 package com.prey.activities;
8 8
9 9 import android.app.AlertDialog;
  10 +
  11 +import com.google.android.apps.analytics.GoogleAnalyticsTracker;
10 12 import com.prey.R;
11 13 import android.app.Dialog;
12 14 import android.app.ProgressDialog;
@@ -111,6 +113,11 @@ protected void onPostExecute(Void unused) {
111 113 String message = getString(R.string.new_account_congratulations_text, email);
112 114 Bundle bundle = new Bundle();
113 115 bundle.putString("message", message);
  116 + GoogleAnalyticsTracker.getInstance().trackEvent(
  117 + "Account", // Category
  118 + "Created", // Action
  119 + "", // Label
  120 + 1);
114 121 Intent intent = new Intent(CreateAccountActivity.this, CongratulationsActivity.class);
115 122 intent.putExtras(bundle);
116 123 startActivity(intent);
25 src/com/prey/activities/PreyActivity.java
@@ -4,7 +4,9 @@
4 4 import android.os.Bundle;
5 5 import android.view.Window;
6 6
  7 +import com.google.android.apps.analytics.GoogleAnalyticsTracker;
7 8 import com.prey.PreyConfig;
  9 +import com.prey.analytics.GoogleAnalyticsSessionManager;
8 10
9 11 public class PreyActivity extends Activity {
10 12
@@ -13,8 +15,31 @@ protected void onCreate(Bundle savedInstanceState) {
13 15 // TODO Auto-generated method stub
14 16 super.onCreate(savedInstanceState);
15 17 requestWindowFeature(Window.FEATURE_NO_TITLE);
  18 +
  19 + // Need to do this for every activity that uses google analytics
  20 + GoogleAnalyticsSessionManager.getInstance(getApplication()).incrementActivityCount();
16 21 }
17 22
  23 +
  24 + @Override
  25 + protected void onResume() {
  26 + super.onResume();
  27 +
  28 + // Example of how to track a pageview event
  29 + GoogleAnalyticsTracker.getInstance().trackPageView(getClass().getSimpleName());
  30 + }
  31 +
  32 + @Override
  33 + protected void onDestroy() {
  34 + super.onDestroy();
  35 +
  36 + // Purge analytics so they don't hold references to this activity
  37 + GoogleAnalyticsTracker.getInstance().dispatch();
  38 +
  39 + // Need to do this for every activity that uses google analytics
  40 + GoogleAnalyticsSessionManager.getInstance().decrementActivityCount();
  41 + }
  42 +
18 43 /*
19 44 @Override
20 45 public void onPause() {
8 src/com/prey/activities/SetupActivity.java
@@ -11,13 +11,13 @@
11 11 import com.prey.PreyUtils;
12 12
13 13 public class SetupActivity extends PreyActivity {
14   -
15   - protected String getDeviceType(){
  14 +
  15 + protected String getDeviceType() {
16 16 return PreyUtils.getDeviceType(this);
17 17 }
18   -
  18 +
19 19 @Override
20   - public void onBackPressed(){
  20 + public void onBackPressed() {
21 21 Intent intent = new Intent(SetupActivity.this, WelcomeActivity.class);
22 22 startActivity(intent);
23 23 finish();
78 src/com/prey/analytics/GoogleAnalyticsSessionManager.java
... ... @@ -0,0 +1,78 @@
  1 +package com.prey.analytics;
  2 +
  3 +import android.app.Application;
  4 +import android.content.Context;
  5 +
  6 +import com.google.android.apps.analytics.GoogleAnalyticsTracker;
  7 +import com.prey.FileConfigReader;
  8 +
  9 +public class GoogleAnalyticsSessionManager {
  10 + protected static GoogleAnalyticsSessionManager INSTANCE;
  11 +
  12 + protected int activityCount = 0;
  13 + protected Integer dispatchIntervalSecs;
  14 + protected String apiKey;
  15 + protected Context context;
  16 +
  17 + /**
  18 + * NOTE: you should use your Application context, not your Activity context, in order to avoid memory leaks.
  19 + */
  20 + protected GoogleAnalyticsSessionManager( String apiKey, Application context ) {
  21 + this.apiKey = apiKey;
  22 + this.context = context;
  23 + }
  24 +
  25 + /**
  26 + * NOTE: you should use your Application context, not your Activity context, in order to avoid memory leaks.
  27 + */
  28 + protected GoogleAnalyticsSessionManager( String apiKey, int dispatchIntervalSecs, Application context ) {
  29 + this.apiKey = apiKey;
  30 + this.dispatchIntervalSecs = dispatchIntervalSecs;
  31 + this.context = context;
  32 + }
  33 +
  34 + /**
  35 + * This should be called once in onCreate() for each of your activities that use GoogleAnalytics.
  36 + * These methods are not synchronized and don't generally need to be, so if you want to do anything
  37 + * unusual you should synchronize them yourself.
  38 + */
  39 + public void incrementActivityCount() {
  40 + if( activityCount==0 )
  41 + if( dispatchIntervalSecs==null )
  42 + GoogleAnalyticsTracker.getInstance().startNewSession(apiKey,context);
  43 + else
  44 + GoogleAnalyticsTracker.getInstance().startNewSession(apiKey,dispatchIntervalSecs,context);
  45 +
  46 + ++activityCount;
  47 + }
  48 +
  49 +
  50 + /**
  51 + * This should be called once in onDestrkg() for each of your activities that use GoogleAnalytics.
  52 + * These methods are not synchronized and don't generally need to be, so if you want to do anything
  53 + * unusual you should synchronize them yourself.
  54 + */
  55 + public void decrementActivityCount() {
  56 + activityCount = Math.max(activityCount-1, 0);
  57 +
  58 + if( activityCount==0 )
  59 + GoogleAnalyticsTracker.getInstance().stopSession();
  60 + }
  61 +
  62 +
  63 + /**
  64 + * Get or create an instance of GoogleAnalyticsSessionManager
  65 + */
  66 + public static GoogleAnalyticsSessionManager getInstance( Application application ) {
  67 + if( INSTANCE == null )
  68 + INSTANCE = new GoogleAnalyticsSessionManager(FileConfigReader.getInstance(application).getAnalyticsUA() ,application);
  69 + return INSTANCE;
  70 + }
  71 +
  72 + /**
  73 + * Only call this if you're sure an instance has been previously created using #getInstance(Application)
  74 + */
  75 + public static GoogleAnalyticsSessionManager getInstance() {
  76 + return INSTANCE;
  77 + }
  78 +}

0 comments on commit a7ada65

Please sign in to comment.
Something went wrong with that request. Please try again.