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

feat: add library context field #76

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions src/main/java/com/amplitude/Amplitude.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Amplitude {
private Object eventQueueLock = new Object();
private Plan plan;
private long flushTimeout;
private String libraryContext;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that adding a new middleware to this repo might be a little overkilled, e.g.:

public class ContextMiddleware implements Middleware {
    public String libraryContext;

    public ContextMiddleware(String libraryContext) {
        this.libraryContext = libraryContext;
    }

    @Override
    public void run(MiddlewarePayload payload, MiddlewareNext next) {
        if (payload.event != null) {
            payload.event.libraryContext = this.libraryContext;
        }
        next.run(payload);
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What'll be your suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my preference would be the change in this PR.

  • The property can be globally set (like plan), doesn't need to be applied per event
  • Lightweight and minimum changes, adding a new middleware and exporting it in package only for this case seems overkill to me

For the Python (new generation SDk) change: https://github.com/amplitude/Amplitude-Python/pull/33/files. Similarly, exposing a way to set it globally, as Python puts the library in ContextPlugin, we can put in the similar place, while keeping the changes minimum and lightweight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Thanks


/**
* A dictionary of key-value pairs that represent additional instructions for server save
Expand Down Expand Up @@ -163,6 +164,16 @@ public Amplitude setPlan(Plan plan) {
return this;
}

/**
* Set library context information.
*
* @param libraryContext String
*/
public Amplitude setLibraryContext(String libraryContext) {
this.libraryContext = libraryContext;
return this;
}

/**
* Set custom proxy for https requests
*
Expand Down Expand Up @@ -242,6 +253,10 @@ public void logEvent(Event event, AmplitudeCallbacks callbacks, MiddlewareExtra
event.plan = this.plan;
}

if (event.libraryContext == null) {
event.libraryContext = this.libraryContext;
}

if (!middlewareRunner.run(new MiddlewarePayload(event, extra))) {
return;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/amplitude/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ public class Event {
*/
public Plan plan;

/**
* The library context information.
*/
public String libraryContext;

/**
* Callback for Event
*/
Expand Down Expand Up @@ -279,6 +284,10 @@ public JSONObject toJsonObject() {
if (plan != null) {
event.put("plan", plan.toJSONObject());
}

if (libraryContext != null) {
event.put("library_context", libraryContext);
}
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/amplitude/AmplitudeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,41 @@ public void testSetPlan()
assertEquals(versionId, result.getString(Constants.AMP_PLAN_VERSION_ID));
}

@Test
public void testSetLibraryContext()
throws NoSuchFieldException, IllegalAccessException, AmplitudeInvalidAPIKeyException,
InterruptedException {
Amplitude amplitude = Amplitude.getInstance("testSetLibraryContext");
amplitude.init(apiKey);

String libraryContext = "test_library_context/1.x";

amplitude.setLibraryContext(libraryContext);

amplitude.useBatchMode(false);
amplitude.setEventUploadThreshold(1);
HttpCall httpCall = getMockHttpCall(amplitude, false);
Response response = ResponseUtil.getSuccessResponse();
CountDownLatch latch = new CountDownLatch(1);

AtomicReference<List<Event>> sentEvents = new AtomicReference<>();
when(httpCall.makeRequest(anyList()))
.thenAnswer(
invocation -> {
sentEvents.set(invocation.getArgument(0));
latch.countDown();
return response;
});

amplitude.logEvent(new Event("test-event", "test-user"));

assertTrue(latch.await(1L, TimeUnit.SECONDS));
assertEquals(1, sentEvents.get().size());

Event sentEvent = sentEvents.get().get(0);
assertEquals(libraryContext, sentEvent.libraryContext);
}

private HttpCall getMockHttpCall(Amplitude amplitude, boolean useBatch)
throws NoSuchFieldException, IllegalAccessException {
HttpCall httpCall = mock(HttpCall.class);
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/amplitude/EventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void testToJsonObject() {
assertEquals(
Constants.SDK_LIBRARY + "/" + Constants.SDK_VERSION, truncatedEvent.getString("library"));
assertEquals(-1, truncatedEvent.getLong("session_id"));
assertFalse(truncatedEvent.has("library_context"));
}

@Test
Expand Down