-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [ZIF-1008] add Matomo feature usage tracking for smart link (#474)
* feat(tracking): add initial layout for tracking an event with Matomo - add test on matomo success + match url * feat(tracking): add failure when Matomo answers != 204 - add test on matomo failure/status code * feat(tracking): add tracking of smartlink event to CreateSmartLinks - small refactors + test on tracking called when creating smart links * test: add test to check equality for TrackingUtil.anonymize * test: inline few local vars * test: make generateRequest return a request instead of a url * chore: make sendEvent ignore failures * chore: apply sonarqube suggestions * feat: remove unnecessary userId hashing --------- Co-authored-by: Giovanni De Facci <giovanni.defacci@zextras.com> Co-authored-by: Alessio Coser <18616553+AlessioCoser@users.noreply.github.com>
- Loading branch information
1 parent
f0481ba
commit f352b31
Showing
11 changed files
with
541 additions
and
248 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
store/src/main/java/com/zextras/mailbox/tracking/Event.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package com.zextras.mailbox.tracking; | ||
|
||
public class Event { | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public String getCategory() { | ||
return category; | ||
} | ||
|
||
public String getAction() { | ||
return action; | ||
} | ||
|
||
private final String userId; | ||
private final String category; | ||
private final String action; | ||
|
||
public Event(String userId, String category, String action) { | ||
this.userId = userId; | ||
this.category = category; | ||
this.action = action; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
store/src/main/java/com/zextras/mailbox/tracking/MatomoTracking.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package com.zextras.mailbox.tracking; | ||
|
||
import java.net.URI; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
|
||
public class MatomoTracking implements Tracking { | ||
|
||
private final String matomoEndpoint; | ||
|
||
public MatomoTracking(String matomoEndpoint) { | ||
this.matomoEndpoint = matomoEndpoint; | ||
} | ||
|
||
@Override | ||
public void sendEventIgnoringFailure(Event event) { | ||
try (CloseableHttpClient client = HttpClientBuilder.create().build()) { | ||
client.execute(generateRequest(event)); | ||
} catch (Exception ignored) { | ||
} | ||
} | ||
|
||
private HttpGet generateRequest(Event event) { | ||
final HttpGet httpGet = new HttpGet(); | ||
String url = matomoEndpoint + "/matomo.php?idsite=7&rec=1&send_image=0&apiv=1" | ||
+ "&e_c=" + event.getCategory() + | ||
"&e_a=" + event.getAction() + | ||
"&uid=" + event.getUserId(); | ||
httpGet.setURI(URI.create(url)); | ||
return httpGet; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
store/src/main/java/com/zextras/mailbox/tracking/Tracking.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package com.zextras.mailbox.tracking; | ||
|
||
/** | ||
* Tracks events/actions performed by the user to use as metrics/insights on Carbonio usages. | ||
* Tracking of data is anonymous and not linkable to user information/personal data. | ||
* | ||
*/ | ||
public interface Tracking { | ||
|
||
void sendEventIgnoringFailure(Event event); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
store/src/test/java/com/zextras/mailbox/soap/SoapUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package com.zextras.mailbox.soap; | ||
|
||
import org.apache.http.HttpResponse; | ||
|
||
public class SoapUtils { | ||
|
||
public static String getResponse(HttpResponse response) throws Exception { | ||
return new String (response.getEntity().getContent().readAllBytes()); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
store/src/test/java/com/zextras/mailbox/tracking/MatomoTrackingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package com.zextras.mailbox.tracking; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.mockserver.integration.ClientAndServer.startClientAndServer; | ||
import static org.mockserver.model.HttpRequest.request; | ||
import static org.mockserver.model.HttpResponse.response; | ||
|
||
import java.io.IOException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockserver.integration.ClientAndServer; | ||
import org.mockserver.model.HttpRequest; | ||
import org.mockserver.model.Parameter; | ||
import org.mockserver.verify.VerificationTimes; | ||
|
||
class MatomoTrackingTest { | ||
|
||
private ClientAndServer matomo; | ||
private static final int MATOMO_PORT = 5000; | ||
|
||
@BeforeEach | ||
public void startUp() throws IOException { | ||
matomo = startClientAndServer(MATOMO_PORT); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() throws IOException { | ||
matomo.stop(); | ||
} | ||
|
||
@Test | ||
void shouldSendEventToMatomo() { | ||
final Event event = new Event("UserId", "TestCategory", "TestAction"); | ||
final HttpRequest matomoRequest = createMatomoRequest("UserId", "TestCategory", "TestAction"); | ||
mockSuccessMatomoResponse(matomoRequest); | ||
|
||
assertDoesNotThrow(() -> new MatomoTracking("http://localhost:" + MATOMO_PORT).sendEventIgnoringFailure(event)); | ||
matomo.verify(matomoRequest, VerificationTimes.exactly(1)); | ||
} | ||
|
||
@Test | ||
void shouldFailWhenMatomoFails() { | ||
final Event event = new Event("UserId", "TestCategory", "TestAction"); | ||
final HttpRequest matomoRequest = createMatomoRequest("UserId", "TestCategory", "TestAction"); | ||
mockFailureMatomoResponse(matomoRequest); | ||
|
||
assertDoesNotThrow(() -> new MatomoTracking("http://localhost:" + MATOMO_PORT).sendEventIgnoringFailure(event)); | ||
matomo.verify(matomoRequest, VerificationTimes.exactly(1)); | ||
} | ||
|
||
private void mockFailureMatomoResponse(HttpRequest request) { | ||
matomo | ||
.when(request) | ||
.respond(response().withStatusCode(500)); | ||
} | ||
|
||
private void mockSuccessMatomoResponse(HttpRequest request) { | ||
matomo | ||
.when(request) | ||
.respond(response().withStatusCode(204)); | ||
} | ||
|
||
private HttpRequest createMatomoRequest(String uid, String category, String action) { | ||
return request() | ||
.withMethod("GET") | ||
.withPath("/matomo.php") | ||
.withQueryStringParameters( | ||
Parameter.param("idsite", "7"), | ||
Parameter.param("rec", "1"), | ||
Parameter.param("send_image", "0"), | ||
Parameter.param("apiv", "1"), | ||
Parameter.param("e_c", category), | ||
Parameter.param("e_a", action), | ||
Parameter.param("uid", uid) | ||
); | ||
} | ||
} |
Oops, something went wrong.