Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/swift-beds-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ledger-live-desktop": patch
---

Fixes analytics support
32 changes: 15 additions & 17 deletions apps/ledger-live-desktop/src/renderer/analytics/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,22 @@ const extraProperties = store => {

let storeInstance; // is the redux store. it's also used as a flag to know if analytics is on or off.

function getAnalytics() {
const { analytics } = window;
if (typeof analytics === "undefined") {
logger.critical(new Error("window.analytics must not be undefined!"));
}
return analytics;
}

export const start = async (store: *) => {
if (!user || (!process.env.SEGMENT_TEST && (process.env.MOCK || process.env.PLAYWRIGHT_RUN)))
return;
const { id } = await user();
logger.analyticsStart(id, extraProperties(store));
storeInstance = store;
const { analytics } = window;
if (typeof analytics === "undefined") {
logger.error("analytics is not available");
return;
}
const analytics = getAnalytics();
if (!analytics) return;
logger.analyticsStart(id, extraProperties(store));
analytics.identify(id, extraProperties(store), {
context: getContext(store),
});
Expand All @@ -86,11 +91,8 @@ export const start = async (store: *) => {
export const stop = () => {
logger.analyticsStop();
storeInstance = null;
const { analytics } = window;
if (typeof analytics === "undefined") {
logger.error("analytics is not available");
return;
}
const analytics = getAnalytics();
if (!analytics) return;
analytics.reset();
};

Expand All @@ -100,12 +102,8 @@ export const trackSubject = new ReplaySubject<{
}>(10);

function sendTrack(event, properties: ?Object, storeInstance: *) {
const { analytics } = window;
if (typeof analytics === "undefined") {
logger.error("analytics is not available");
return;
}

const analytics = getAnalytics();
if (!analytics) return;
analytics.track(event, properties, {
context: getContext(storeInstance),
});
Expand Down
2 changes: 1 addition & 1 deletion apps/ledger-live-desktop/src/renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<html>
<head>
<meta charset="utf-8" />
<script async type="module" src="./index.js" />
<script>
!(function() {
const analytics = (window.analytics = window.analytics || []);
Expand Down Expand Up @@ -45,6 +44,7 @@
}
})();
</script>
<script defer type="module" src="./index.js"></script>
<script
async
src="https://resources.live.ledger.app/public_resources/analytics.min.js"
Expand Down
19 changes: 18 additions & 1 deletion apps/ledger-live-desktop/tests/specs/analytics/segment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,29 @@ test("Segment", async ({ page }) => {
});
});

await test.step("has analytics.Integrations defined", async () => {
const firstAnalyticsEventRequestData: Promise<{ userId?: string }> = new Promise(resolve => {
page.on("request", request => {
if (request.url() === "https://api.segment.io/v1/t") {
const data = request.postDataJSON();
resolve(data);
}
});
});

await test.step("has analytics settled", async () => {
const hasAnalytics = await page.evaluate(() => !!(window as any).analytics);
expect(hasAnalytics).toBe(true);
await page.waitForTimeout(1000); // give 1s more for analytics.min.js in case it doesn't load
await layout.goToSettings();
const hasIntegrations = await page.evaluate(() => "Integrations" in (window as any).analytics);
expect(hasIntegrations).toBe(true);
});

await test.step("has sent event with userId settled", async () => {
const data = await firstAnalyticsEventRequestData;
expect(data?.userId).toBeTruthy();
});

await test.step("has sent events in HTTP", async () => {
expect(await firstSuccessfulQuery).toBeDefined();
});
Expand Down