Skip to content

Commit

Permalink
improve android analytics data
Browse files Browse the repository at this point in the history
  • Loading branch information
irov committed Oct 25, 2023
1 parent 7af9207 commit af575d7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

public class MengineAnalytics {
private static MengineApplication m_application;
private static Map<String, Object> m_context = new HashMap<>();
private static Map<String, Object> m_bases = new HashMap<>();
private static Map<String, Object> m_getter = new HashMap<>();

public static void setMengineApplication(MengineApplication application) {
MengineAnalytics.m_application = application;
}

static private void assertContext(String key) {
if (MengineAnalytics.m_context.containsKey(key) == true) {
if (MengineAnalytics.m_bases.containsKey(key) == true) {
String msg = String.format(Locale.US, "global analytics context parameter '%s' already exist", key);
throw new AssertionError(msg);
}
Expand All @@ -33,28 +33,28 @@ static public void addContextParameterBoolean(@Size(min = 1L,max = 40L) String k
MengineAnalytics.assertContext(key);
MengineAnalytics.assertGetter(key);

MengineAnalytics.m_context.put(key, value);
MengineAnalytics.m_bases.put(key, value);
}

static public void addContextParameterString(@Size(min = 1L,max = 40L) String key, @Size(min = 1L,max = 100L) String value) {
MengineAnalytics.assertContext(key);
MengineAnalytics.assertGetter(key);

MengineAnalytics.m_context.put(key, value);
MengineAnalytics.m_bases.put(key, value);
}

static public void addContextParameterLong(@Size(min = 1L,max = 40L) String key, long value) {
MengineAnalytics.assertContext(key);
MengineAnalytics.assertGetter(key);

MengineAnalytics.m_context.put(key, value);
MengineAnalytics.m_bases.put(key, value);
}

static public void addContextParameterDouble(@Size(min = 1L,max = 40L) String key, double value) {
MengineAnalytics.assertContext(key);
MengineAnalytics.assertGetter(key);

MengineAnalytics.m_context.put(key, value);
MengineAnalytics.m_bases.put(key, value);
}

static public void addContextGetterParameterString(@Size(min = 1L,max = 40L) String key, MengineAnalyticsGetter<String> value) {
Expand Down Expand Up @@ -94,7 +94,7 @@ static public MengineAnalyticsEventBuilder buildEvent(@Size(min = 1L,max = 40L)

MengineAnalytics.collapseGetter(parameters);

MengineAnalyticsEventBuilder eventBuilder = new MengineAnalyticsEventBuilder(MengineAnalytics.m_application, MengineAnalytics.m_context, parameters, name);
MengineAnalyticsEventBuilder eventBuilder = new MengineAnalyticsEventBuilder(MengineAnalytics.m_application, MengineAnalytics.m_bases, parameters, name);

return eventBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ public String getBuildUsername() {

private String m_installKey;
private long m_installTimestamp = -1;
private String m_installDate;
private String m_installVersion;
private long m_installRND = -1;
private long m_sessionIndex = -1;
private String m_sessionId;
private long m_sessionTimestamp;
private String m_sessionDate;
private long m_sessionTimestamp = -1;

private ArrayList<MenginePlugin> m_plugins = new ArrayList<>();
private Map<String, MenginePlugin> m_dictionaryPlugins = new HashMap<>();
Expand Down Expand Up @@ -213,10 +211,6 @@ public long getInstallTimestamp() {
return m_installTimestamp;
}

public String getInstallDate() {
return m_installDate;
}

public String getInstallVersion() { return m_installVersion; }

public long getInstallRND() {
Expand Down Expand Up @@ -252,10 +246,6 @@ public long getSessionTimestamp() {
return m_sessionTimestamp;
}

public String getSessionDate() {
return m_sessionDate;
}

public String getDeviceLanguage() {
String language = Locale.getDefault().getLanguage();

Expand Down Expand Up @@ -674,8 +664,7 @@ public void onCreate() {
SharedPreferences settings = this.getPrivateSharedPreferences(TAG);

String installKey = settings.getString("install_key", null);
long installTimestamp = settings.getLong("install_timestamp", 0);
String installDate = settings.getString("install_date", "");
long installTimestamp = settings.getLong("install_timestamp", -1);
String installVersion = settings.getString("install_version", "");
long installRND = settings.getLong("install_rnd", -1);
long sessionIndex = settings.getLong("session_index", 0);
Expand All @@ -686,12 +675,10 @@ public void onCreate() {
if (installKey == null) {
installKey = MengineUtils.getRandomUUIDString();
installTimestamp = MengineUtils.getTimestamp();
installDate = MengineUtils.getDateFormat("d MMM yyyy HH:mm:ss");
installVersion = this.getVersionName();

editor.putString("install_key", installKey);
editor.putLong("install_timestamp", installTimestamp);
editor.putString("install_date", installDate);
editor.putString("install_version", installVersion);
}

Expand All @@ -718,13 +705,18 @@ public void onCreate() {

m_installKey = installKey;
m_installTimestamp = installTimestamp;
m_installDate = installDate;
m_installVersion = installVersion;
m_installRND = installRND;
m_sessionIndex = sessionIndex;
m_sessionId = sessionId;
m_sessionTimestamp = MengineUtils.getTimestamp();
m_sessionDate = MengineUtils.getDateFormat("d MMM yyyy HH:mm:ss");

MengineAnalytics.addContextParameterString("install_key", m_installKey);
MengineAnalytics.addContextParameterLong("install_timestamp", m_installTimestamp);
MengineAnalytics.addContextParameterString("install_version", m_installVersion);
MengineAnalytics.addContextParameterLong("install_rnd", m_installRND);
MengineAnalytics.addContextParameterLong("session_index", m_sessionIndex);
MengineAnalytics.addContextParameterLong("session_timestamp", m_sessionTimestamp);

String build_gitsha1 = this.getEngineGITSHA1();
this.setState("engine.build_gitsha1", build_gitsha1);
Expand All @@ -737,13 +729,17 @@ public void onCreate() {

this.setState("user.install_key", m_installKey);
this.setState("user.install_timestamp", m_installTimestamp);
this.setState("user.install_date", m_installDate);
this.setState("user.install_version", m_installVersion);
this.setState("user.install_rnd", m_installRND);
this.setState("user.session_index", m_sessionIndex);
this.setState("user.session_id", m_sessionId);
this.setState("user.session_timestamp", m_sessionTimestamp);
this.setState("user.session_date", m_sessionDate);

String installDate = MengineUtils.getDateFormat(m_installTimestamp, "d MMM yyyy HH:mm:ss");
this.setState("user.install_date", installDate);

String sessionDate = MengineUtils.getDateFormat(m_sessionTimestamp, "d MMM yyyy HH:mm:ss");
this.setState("user.session_date", sessionDate);

this.setState("application.init", "load");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ public static Uri getUriForFile(Context context, File file) {
return null;
}

public static String getDateFormat(String format) {
public static String getDateFormat(long timestamp, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
Date date = Calendar.getInstance().getTime();
Date date = new Date(timestamp);
String s = df.format(date);

return s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,21 @@ public void onAppPrepare(MengineApplication application) throws MenginePluginInv
String installKey = application.getInstallKey();
loggerDataDog.addAttribute("install_key", installKey);

long installTimestamp = application.getInstallTimestamp();
loggerDataDog.addAttribute("install_timestamp", installTimestamp);

String installVersion = application.getInstallVersion();
loggerDataDog.addAttribute("install_version", installVersion);

long installRND = application.getInstallRND();
loggerDataDog.addAttribute("install_rnd", installRND);

long sessionIndex = application.getSessionIndex();
loggerDataDog.addAttribute("session_index", sessionIndex);

long sessionTimestamp = application.getSessionTimestamp();
loggerDataDog.addAttribute("session_timestamp", sessionTimestamp);

m_loggerDataDog = loggerDataDog;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ public void onAppPrepare(MengineApplication application) throws MenginePluginInv
long sessionIndex = application.getSessionIndex();
long sessionTimestamp = application.getSessionTimestamp();

Bundle params = new Bundle();
params.putString("install_key", installKey);
params.putLong("install_timestamp", installTimestamp);
params.putString("install_version", installVersion);
params.putLong("install_rnd", installRND);
params.putLong("session_index", sessionIndex);
params.putLong("session_timestamp", sessionTimestamp);

m_firebaseAnalytics.setDefaultEventParameters(params);

m_firebaseAnalytics.setUserProperty("is_dev", String.valueOf(BuildConfig.DEBUG));
m_firebaseAnalytics.setUserProperty("install_key", installKey);
m_firebaseAnalytics.setUserProperty("install_timestamp", String.valueOf(installTimestamp));
Expand Down

0 comments on commit af575d7

Please sign in to comment.