Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Implement the new module for view tracking #182

Merged
merged 46 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
3cc180b
feat: init view module
arifBurakDemiray Dec 4, 2023
2f1f368
feat: module views
arifBurakDemiray Dec 5, 2023
7bdd387
refactor: delete unused things
arifBurakDemiray Dec 5, 2023
8a00406
refactor: delete unnecessary things for views java
arifBurakDemiray Dec 5, 2023
f3c7353
refactor: delete unnecessary things for views java
arifBurakDemiray Dec 5, 2023
c9d5de1
feat: views to the flow
arifBurakDemiray Dec 5, 2023
bb5c37f
Merge branch 'staging' into new_view_module
arifBurakDemiray Dec 7, 2023
7423108
fix: missing thingies
arifBurakDemiray Dec 7, 2023
d560fbf
Merge branch 'staging' into new_view_module
arifBurakDemiray Dec 18, 2023
3c961e3
fix: missing curly bracket
arifBurakDemiray Dec 18, 2023
c7b30eb
fix: remove _idv
arifBurakDemiray Dec 18, 2023
3ced2c6
feat: new functions
arifBurakDemiray Dec 18, 2023
ffa0932
fix: view segmentation
arifBurakDemiray Dec 19, 2023
7da3d55
fix: convert to linekd hash map
arifBurakDemiray Dec 19, 2023
3efa3bf
feat: move convenient functions to utils
arifBurakDemiray Dec 19, 2023
9120c8d
Merge pull request #192 from Countly/move_functions
arifBurakDemiray Dec 19, 2023
f120c02
fix: remove unnecesarry check
arifBurakDemiray Dec 19, 2023
428e6c6
feat: session changes
arifBurakDemiray Dec 19, 2023
8830035
fix: delete unncessary
arifBurakDemiray Dec 19, 2023
41427ab
fix: whitespace
arifBurakDemiray Dec 19, 2023
45964c6
fix: some of orders
arifBurakDemiray Dec 20, 2023
4ca30d4
feat: random val
arifBurakDemiray Dec 20, 2023
216766e
Update ModuleViews.java
arifBurakDemiray Dec 20, 2023
1913a1d
Update ModuleViews.java
arifBurakDemiray Dec 20, 2023
1090467
Update SDKCore.java
arifBurakDemiray Dec 20, 2023
a767a52
Update UtilsTests.java
arifBurakDemiray Dec 20, 2023
299b395
Merge pull request #199 from Countly/staging
arifBurakDemiray Jan 3, 2024
4de8422
fix: revert test
arifBurakDemiray Jan 3, 2024
2ba6412
fix: changes from child pr
arifBurakDemiray Jan 3, 2024
7b05172
Merge branch 'staging' into new_view_module
arifBurakDemiray Jan 4, 2024
d45cea3
feat: migrate test branch logic to main
arifBurakDemiray Jan 4, 2024
a8f684c
feat: ids
arifBurakDemiray Jan 5, 2024
1e9179c
feat: usage of ids
arifBurakDemiray Jan 5, 2024
e204b9f
feat: revert force send
arifBurakDemiray Jan 5, 2024
d02df42
Merge branch 'staging' into new_view_module
arifBurakDemiray Jan 9, 2024
7f65a1e
fix: nonull
arifBurakDemiray Jan 9, 2024
06acbc0
feat: migrate from test branch
arifBurakDemiray Jan 9, 2024
64d2e58
Merge branch 'staging' into new_view_module
arifBurakDemiray Jan 15, 2024
00066bc
Merge branch 'staging' into new_view_module
arifBurakDemiray Jan 18, 2024
3cdb82e
feat: global segmns
arifBurakDemiray Jan 23, 2024
c2d2f46
feat: add log
arifBurakDemiray Jan 23, 2024
62ca215
feat: add is empty checl
arifBurakDemiray Jan 23, 2024
44d4295
fix: pr things
arifBurakDemiray Jan 30, 2024
d2fb6da
feat: add old way call
arifBurakDemiray Jan 31, 2024
171052f
feat: test changes
arifBurakDemiray Jan 31, 2024
7cce4bc
[Java] Implement the new module for view tracking - tests (#236)
arifBurakDemiray Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* "setLocale(String)"

* Added the user profiles feature interface, and it is accessible through "Countly::instance()::userProfile()" call.

* Added the location feature interface, and it is accessible through "Countly::instance()::location()" call.
* Added init time configuration for the location parameters:
* "setLocation(String countryCode, String city, String location, String ipAddress)"
* "setDisableLocation()"
* Crash Reporting interface added and accessible through "Countly::instance()::crash()" call.
* Added "disableUnhandledCrashReporting" function to the "Config" class to disable automatic uncaught crash reporting.
* Added "setMaxBreadcrumbCount(int)" function to the "Config" class to change allowed max breadcrumb count.
* Added the views feature interface, and it is accessible through "Countly::instance()::views()" call.

* Fixed a bug where setting custom user properties would not work.
* Fixed a bug where setting organization of the user would not work.
Expand Down Expand Up @@ -46,6 +46,12 @@
* "setCustom(String, Object)" instead use "Countly::userProfile::setProperty" via "instance()" call
* "set(String, Object)" instead use "Countly::userProfile::setProperty" via "instance()" call
* "picture(byte[])" instead use "Countly::userProfile::setProperty" via "instance()" call
* Deprecated "View::start(bool)" call, use "Countly::views::startView" instead via "instance()" call.
arifBurakDemiray marked this conversation as resolved.
Show resolved Hide resolved
* Deprecated "View::stop(bool)" call, use "Countly::views::stopViewWithName" or "Countly::views::stopViewWithID" instead via "instance()" call.
* Deprecated "Usage::view(String)" call, use "Countly::views::startView" instead via "instance()" call.
* Deprecated "Usage::view(String, bool)" call, use "Countly::views::startView" instead via "instance()" call.
* Deprecated "Countly::view(String)" call, use "Countly::views::startView" instead via "instance()" call.
* Deprecated "Countly::view(String, bool)" call, use "Countly::views::startView" instead via "instance()" call.

## 23.10.1

Expand Down
16 changes: 16 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/Countly.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ly.count.sdk.java.internal.ModuleLocation;
import ly.count.sdk.java.internal.ModuleRemoteConfig;
import ly.count.sdk.java.internal.ModuleUserProfile;
import ly.count.sdk.java.internal.ModuleViews;
import ly.count.sdk.java.internal.SDKCore;

/**
Expand Down Expand Up @@ -475,6 +476,21 @@ public ModuleCrashes.Crashes crashes() {
return sdk.crashes();
}

/**
* <code>Views</code> interface to use views feature.
*
* @return {@link ModuleViews.Views} instance.
*/
public ModuleViews.Views views() {
if (!isInitialized()) {
if (L != null) {
L.e("[Countly] SDK is not initialized yet.");
}
return null;
}
return sdk.views();
}

/**
* Get existing or create new timed event object, don't record it.
*
Expand Down
1 change: 1 addition & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/View.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package ly.count.sdk.java;
import ly.count.sdk.java.internal.ModuleViews;

import ly.count.sdk.java.internal.ModuleViews;

Expand Down
46 changes: 45 additions & 1 deletion sdk-java/src/main/java/ly/count/sdk/java/internal/EventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class EventImpl implements Event, JSONable {
protected int hour;
protected int dow;

protected String id;
protected String pvid;
protected String cvid;
protected String peid;

final Log L;

public interface EventRecorder {
Expand All @@ -38,7 +43,7 @@ public interface EventRecorder {
*/
private boolean invalid = false;

EventImpl(@Nonnull String key, int count, Double sum, Double duration, @Nonnull Map<String, Object> segmentation, @Nonnull Log givenL) {
EventImpl(@Nonnull String key, int count, Double sum, Double duration, @Nonnull Map<String, Object> segmentation, @Nonnull Log givenL, String id, String pvid, String cvid, String peid) {
L = givenL;

this.recorder = null;
Expand All @@ -51,6 +56,10 @@ public interface EventRecorder {
this.timestamp = instant.timestamp;
this.hour = instant.hour;
this.dow = instant.dow;
this.id = id;
this.pvid = pvid;
this.cvid = cvid;
this.peid = peid;
}

EventImpl(@Nonnull EventRecorder recorder, @Nonnull String key, @Nonnull Log givenL) {
Expand Down Expand Up @@ -239,6 +248,10 @@ public boolean equals(Object obj) {
protected static final String TIMESTAMP_KEY = "timestamp";
protected static final String DAY_OF_WEEK = "dow";
protected static final String HOUR = "hour";
protected static final String ID_KEY = "id";
protected static final String PV_ID_KEY = "pvid";
protected static final String CV_ID_KEY = "cvid";
protected static final String PE_ID_KEY = "peid";

/**
* Serialize to JSON format according to Countly server requirements
Expand Down Expand Up @@ -266,6 +279,23 @@ public String toJSON(@Nonnull Log log) {
if (duration != null) {
json.put(DUR_KEY, duration);
}

//set the ID's only if they are not 'null'
if (id != null) {
json.put(ID_KEY, id);
}

if (pvid != null) {
json.put(PV_ID_KEY, pvid);
}

if (cvid != null) {
json.put(CV_ID_KEY, cvid);
}

if (peid != null) {
json.put(PE_ID_KEY, peid);
}
} catch (JSONException e) {
log.e("[EventImpl] Cannot serialize event to JSON " + e);
}
Expand Down Expand Up @@ -304,6 +334,20 @@ public void recordEvent(Event event) {
event.hour = json.optInt(HOUR);
event.dow = json.optInt(DAY_OF_WEEK);

// the parsed ID's might not be set, or it might be set as null
if (!json.isNull(ID_KEY)) {
event.id = json.getString(ID_KEY);
}
if (!json.isNull(PV_ID_KEY)) {
event.pvid = json.getString(PV_ID_KEY);
}
if (!json.isNull(CV_ID_KEY)) {
event.cvid = json.getString(CV_ID_KEY);
}
if (!json.isNull(PE_ID_KEY)) {
event.peid = json.getString(PE_ID_KEY);
}

if (!json.isNull(SEGMENTATION_KEY)) {
final JSONObject segm = json.getJSONObject(SEGMENTATION_KEY);
final HashMap<String, Object> segmentation = new HashMap<>(segm.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class InternalConfig extends Config {
public StorageProvider storageProvider;
protected IdGenerator viewIdGenerator;
protected IdGenerator eventIdGenerator;

protected ViewIdProvider viewIdProvider;
/**
* Shouldn't be used!
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import java.util.stream.Collectors;
import ly.count.sdk.java.Countly;
import ly.count.sdk.java.Session;
import ly.count.sdk.java.View;

public class ModuleEvents extends ModuleBase {
protected EventQueue eventQueue = null;
final Map<String, EventImpl> timedEvents = new ConcurrentHashMap<>();
protected Events eventsInterface = null;
ViewIdProvider viewIdProvider = null;
IdGenerator idGenerator = null;
String previousEventId = null;

@Override
public void init(InternalConfig config) {
Expand All @@ -20,6 +22,14 @@ public void init(InternalConfig config) {
eventQueue = new EventQueue(L, config.getEventsBufferSize());
eventQueue.restoreFromDisk();
eventsInterface = new Events();

idGenerator = config.eventIdGenerator;
}

@Override
public void initFinished(InternalConfig config) {
super.initFinished(config);
viewIdProvider = config.viewIdProvider;
}

@Override
Expand Down Expand Up @@ -102,20 +112,43 @@ protected void removeInvalidDataFromSegments(Map<String, Object> segments) {
});
}

protected void recordEventInternal(String key, int count, Double sum, Double dur, Map<String, Object> segmentation) {
protected void recordEventInternal(String key, int count, Double sum, Double dur, Map<String, Object> segmentation, String eventIdOverride) {
if (count <= 0) {
L.w("[ModuleEvents] recordEventInternal: Count can't be less than 1, ignoring this event.");
L.w("[ModuleEvents] recordEventInternal, Count can't be less than 1, ignoring this event.");
return;
}

if (key == null || key.isEmpty()) {
L.w("[ModuleEvents] recordEventInternal: Key can't be null or empty, ignoring this event.");
L.w("[ModuleEvents] recordEventInternal, Key can't be null or empty, ignoring this event.");
return;
}

L.d("[ModuleEvents] recordEventInternal, Recording event with key: [" + key + "] and provided event ID of:[" + eventIdOverride + "] and segmentation with:[" + (segmentation == null ? "null" : segmentation.size()) + "] keys");

removeInvalidDataFromSegments(segmentation);
EventImpl event = new EventImpl(key, count, sum, dur, segmentation, L);
addEventToQueue(event);

String eventId, pvid = null, cvid = null;
if (Utils.isEmptyOrNull(eventIdOverride)) {
L.d("[ModuleEvents] recordEventInternal, Generating new event id because it was null or empty");
eventId = idGenerator.generateId();
} else {
eventId = eventIdOverride;
}

if (key.equals(ModuleViews.KEY_VIEW_EVENT)) {
pvid = viewIdProvider.getPreviousViewId();
} else {
cvid = viewIdProvider.getCurrentViewId();
}

String previousEventIdToSend = this.previousEventId;
if (key.equals(FeedbackWidgetType.nps.eventKey) || key.equals(FeedbackWidgetType.survey.eventKey) || key.equals(ModuleViews.KEY_VIEW_EVENT) || key.equals(FeedbackWidgetType.rating.eventKey)) {
previousEventIdToSend = null;
} else {
this.previousEventId = eventId;
}

addEventToQueue(new EventImpl(key, count, sum, dur, segmentation, L, eventId, pvid, cvid, previousEventIdToSend));
}

private void addEventToQueue(EventImpl event) {
Expand Down Expand Up @@ -148,7 +181,7 @@ boolean startEventInternal(final String key) {
L.w("startEventInternal, eventRecorder, No timed event with the name [" + key + "] is started, nothing to end. Will ignore call.");
return;
}
recordEventInternal(eventImpl.key, eventImpl.count, eventImpl.sum, eventImpl.duration, eventImpl.segmentation);
recordEventInternal(eventImpl.key, eventImpl.count, eventImpl.sum, eventImpl.duration, eventImpl.segmentation, eventImpl.id);
}, key, L));

return true;
Expand Down Expand Up @@ -179,7 +212,7 @@ boolean endEventInternal(final String key, final Map<String, Object> segmentatio
long currentTimestamp = TimeUtils.timestampMs();
double duration = (currentTimestamp - event.timestamp) / 1000.0;

recordEventInternal(key, count, sum, duration, segmentation);
recordEventInternal(key, count, sum, duration, segmentation, null);
return true;
}

Expand Down Expand Up @@ -207,7 +240,7 @@ public class Events {
*/
public void recordEvent(String key, Map<String, Object> segmentation, int count, Double sum, Double dur) {
L.i("[Events] recordEvent: key = " + key + ", count = " + count + ", sum = " + sum + ", segmentation = " + segmentation + ", dur = " + dur);
recordEventInternal(key, count, sum, dur, segmentation);
recordEventInternal(key, count, sum, dur, segmentation, null);
}

/**
Expand Down
Loading
Loading