Skip to content

Commit

Permalink
Merge pull request #7 from kissmetrics/autoRecordInstalls-refactor
Browse files Browse the repository at this point in the history
Refactoring of autoRecordInstalls.
  • Loading branch information
willrust committed May 31, 2014
2 parents db43a8c + fa0b4b1 commit 21efdff
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion KISSmetricsAPI/src/com/kissmetrics/sdk/ArchiverImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class ArchiverImpl implements Archiver {

private static final String CLIENT_TYPE = "mobile_app";
private static final String USER_AGENT = "kissmetrics-android/2.0";
private static final String USER_AGENT = "kissmetrics-android/2.0.4";
private static final String IDENTITY_PREF = "KISSmetricsIdentity";
private static final String SETTINGS_FILE = "KISSmetricsSettings";
private static final String INSTALL_UUID_KEY = "installUuid";
Expand Down
2 changes: 1 addition & 1 deletion KISSmetricsAPI/src/com/kissmetrics/sdk/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void sendRecord(final String urlString, final ConnectionDelegate delegate
connection.setUseCaches(false);
connection.setRequestMethod("GET");
connection.setConnectTimeout(CONNECTION_TIMEOUT*1000);
connection.setRequestProperty("User-Agent", "KISSmetrics-Android/2.0.3");
connection.setRequestProperty("User-Agent", "KISSmetrics-Android/2.0.4");
// TODO: Apply any easily obtainable device/OS info to the user agent value

// addressing java.io.EOFException
Expand Down
33 changes: 21 additions & 12 deletions KISSmetricsAPI/src/com/kissmetrics/sdk/KISSmetricsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ public void run() {
}).start();
}

/**
* @return app version from package info
*/
private String appVersionName() {
try {
PackageManager pkgManager = context.getPackageManager();
String pkg = context.getPackageName();
return pkgManager.getPackageInfo(pkg, 0).versionName;
} catch (Exception e) {
return null;
}
}

/**
* Starts sending to empty the sendQueue when sender is in the
Expand Down Expand Up @@ -311,31 +323,30 @@ public void setDistinct(final String propertyName, final String value) {
dataExecutor.execute(trackingRunnables.setDistinct(propertyName,
value, ArchiverImpl.sharedArchiver(), this));
}

/**
* Automatically records the following events "Installed App" "Updated App"
*/
public void autoRecordInstalls() {

PackageManager pkgManager = context.getPackageManager();
String versionName = "";

try {
String pkg = context.getPackageName();
versionName = pkgManager.getPackageInfo(pkg, 0).versionName;
String versionName = appVersionName();

if (versionName == null) {
versionName = "";
} else {
setDistinct("App Version", versionName);
} catch (Exception e) {
// Catch intentionally blank
}

// There is no reliable place to store data that will persist between
// app install and uninstall. We use Archiver's settings store.
String lastAppVersion = ArchiverImpl.sharedArchiver().getAppVersion();

if (versionName.equals(lastAppVersion)) {
// Most common case. No action required.
return;
}

ArchiverImpl.sharedArchiver().archiveAppVersion(versionName);

if (lastAppVersion == null) {
// This is a fresh install
Expand All @@ -344,8 +355,6 @@ public void autoRecordInstalls() {
// This is an update
record("Updated App");
}

ArchiverImpl.sharedArchiver().archiveAppVersion(versionName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ArchiverImplActTest extends ActivityTestCase {
static QueryEncoder queryEncoder;

static String key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
static String userAgent = "kissmetrics-android/2.0";
static String userAgent = "kissmetrics-android/2.0.4";
static String clientType = "mobile_app";


Expand Down
Binary file modified KISSmetricsSDK.jar
Binary file not shown.

0 comments on commit 21efdff

Please sign in to comment.