Skip to content

Commit

Permalink
Added Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
xRahul committed Jan 15, 2017
1 parent f078f94 commit 87557aa
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
/build
/captures
.externalNativeBuild

app/google-services.json
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added app/APKs/app-release 1.2.apk
Binary file not shown.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 23
Expand Down Expand Up @@ -29,5 +30,6 @@ dependencies {
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:support-vector-drawable:23.4.0'
compile 'com.google.android.gms:play-services-analytics:9.0.0'
testCompile 'junit:junit:4.12'
}
38 changes: 38 additions & 0 deletions app/google-services (demo).json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"project_info": {
"project_number": "11111111111",
"project_id": "ficsave-middleware"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:11111111111:android:qaaqaqaqaqaqaq",
"android_client_info": {
"package_name": "in.rahulja.ficsavemiddleware"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "SDsdfJsfSdHFsdfGsdfDKdfSdfFHdG"
}
],
"services": {
"analytics_service": {
"status": 2,
"analytics_property": {
"tracking_id": "UA-11111111-1"
}
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 1
}
}
}
],
"configuration_version": "1"
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package="in.rahulja.ficsavemiddleware">

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

<application
Expand All @@ -12,6 +13,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".FicsaveMiddlewareApplication"
tools:node="replace"
android:theme="@style/AppTheme">
<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;


public class FicSettingsActivity extends PreferenceActivity {

private Tracker mTracker;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -16,5 +21,16 @@ public void onCreate(Bundle savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
FicsaveMiddlewareApplication application = (FicsaveMiddlewareApplication) getApplication();
mTracker = application.getDefaultTracker();
}

@Override
public void onResume() {
super.onResume();
mTracker.setScreenName("Settings");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
import android.webkit.URLUtil;
import android.widget.Toast;

import java.io.File;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

class FicsaveDownloadListener implements DownloadListener {
public static final String OPEN_FILE_PREFERENCE = "open_file_preference";
public static final String SEND_EMAIL_DEVICE_PREFERENCE = "send_email_device_preference";
public static final String EMAIL_ADDRESS_TO_SEND_TO = "email_address_to_send_to";
private static final String OPEN_FILE_PREFERENCE = "open_file_preference";
private static final String SEND_EMAIL_DEVICE_PREFERENCE = "send_email_device_preference";
private static final String EMAIL_ADDRESS_TO_SEND_TO = "email_address_to_send_to";
private final SharedPreferences prefs;
private MainActivity mContext;
private DownloadManager mDownloadManager;
Expand All @@ -34,10 +35,13 @@ class FicsaveDownloadListener implements DownloadListener {

private long fileDownloadId;
private String fileName;
private Tracker mTracker;

FicsaveDownloadListener(MainActivity context) {
mContext = context;
prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
FicsaveMiddlewareApplication application = (FicsaveMiddlewareApplication) mContext.getApplication();
mTracker = application.getDefaultTracker();
}

@Override
Expand All @@ -54,36 +58,6 @@ public void onDownloadStart(String url, String userAgent, String

downloadFile();
listenToDownloadComplete();

/*
mRequest = new DownloadManager.Request(Uri.parse(url));
// Limits the download to only over WiFi. Optional.
mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// Makes download visible in notifications while downloading, but disappears after download completes. Optional.
mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
mRequest.setMimeType(mimetype);
// If necessary for a security check. I needed it, but I don't think it's mandatory.
String cookie = CookieManager.getInstance().getCookie(url);
mRequest.addRequestHeader("Cookie", cookie);
// Grabs the file name from the Content-Disposition
String filename = null;
Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")");
Matcher regexMatcher = regex.matcher(contentDisposition);
if (regexMatcher.find()) {
filename = regexMatcher.group();
}
// Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!!
mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename);
// Sets the title of the notification and how it appears to the user in the saved directory.
mRequest.setTitle(filename);
// Adds the request to the DownloadManager queue to be executed at the next available opportunity.
mDownloadedFileID = mDownloadManager.enqueue(mRequest);
*/
}

private void listenToDownloadComplete() {
Expand Down Expand Up @@ -151,6 +125,13 @@ public void onReceive(Context context, Intent intent) {
fileDownloadId = -1;

mContext.unregisterReceiver(this);

mTracker.send(new HitBuilders.EventBuilder()
.setCategory("DownloadListenerCategory")
.setAction("Download Complete")
.setLabel("File: " + fileName)
.setValue(1)
.build());
}
};
// Registers function to listen to the completion of the download.
Expand Down Expand Up @@ -191,5 +172,12 @@ private void downloadFile() {
Log.d("ficsaveM/DownloadStartd", "fileID: " + fileDownloadId + ", fileName: " + fileName);
Toast.makeText(mContext, R.string.downloading_file_toast_msg, //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();

mTracker.send(new HitBuilders.EventBuilder()
.setCategory("DownloadListenerCategory")
.setAction("Download Enqueued")
.setLabel("File: " + fileName)
.setValue(1)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package in.rahulja.ficsavemiddleware;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;


public class FicsaveMiddlewareApplication extends Application {
private Tracker mTracker;

/**
* Gets the default {@link Tracker} for this {@link Application}.
* @return tracker
*/
synchronized public Tracker getDefaultTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
mTracker = analytics.newTracker(R.xml.global_tracker);
}
return mTracker;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;


class FicsaveWebViewClient extends WebViewClient {

private MainActivity mActivity;
private Tracker mTracker;

FicsaveWebViewClient(MainActivity activity) {
mActivity = activity;
FicsaveMiddlewareApplication application = (FicsaveMiddlewareApplication) mActivity.getApplication();
mTracker = application.getDefaultTracker();
}

@Override
Expand All @@ -24,8 +30,17 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(mActivity.getString(R.string.ficsave_host))) {
return false;
}

mTracker.send(new HitBuilders.EventBuilder()
.setCategory("WebViewClientCategory")
.setAction("Other Url than ficsave Clicked")
.setLabel("Url: " + url)
.setValue(1)
.build());

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
mActivity.startActivity(intent);

return true;
}

Expand All @@ -45,5 +60,11 @@ public void onPageFinished(final WebView view, String url) {
@SuppressWarnings("deprecation")
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.d("ficsaveM/ErrorLoading", "Url: " + failingUrl + " Reason" + description);
mTracker.send(new HitBuilders.EventBuilder()
.setCategory("WebViewClientCategory")
.setAction("Page Load Error")
.setLabel("Url: " + failingUrl + " Reason" + description)
.setValue(1)
.build());
}
}
Loading

0 comments on commit 87557aa

Please sign in to comment.