Skip to content

Commit 092ad93

Browse files
author
Jonah Paten
authored
feat!: added file download tracking (#260) (#263)
* feat: added file download tracking (#260) * feat!: made tracking parameters to azulFileDownload required (#260)
1 parent b6d3efa commit 092ad93

File tree

7 files changed

+55
-15
lines changed

7 files changed

+55
-15
lines changed

src/common/analytics/entities.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export enum EVENT_NAME {
99
ENTITY_SELECTED = "entity_selected",
1010
ENTITY_TABLE_PAGINATED = "entity_table_paginated",
1111
ENTITY_TABLE_SORTED = "entity_table_sorted",
12+
FILE_DOWNLOADED = "file_downloaded",
1213
FILTER_SELECTED = "filter_selected",
1314
INDEX_ANALYZE_IN_TERRA_REQUESTED = "index_analyze_in_terra_requested",
1415
INDEX_FILE_MANIFEST_REQUESTED = "index_file_manifest_requested",
@@ -23,6 +24,8 @@ export enum EVENT_PARAM {
2324
FILTER_NAME = "filter_name",
2425
FILTER_VALUE = "filter_value",
2526
PAGINATION_DIRECTION = "pagination_direction",
27+
RELATED_ENTITY_ID = "related_entity_id",
28+
RELATED_ENTITY_NAME = "related_entity_name",
2629
SORT_DIRECTION = "sort_direction",
2730
TOOL_NAME = "tool_name",
2831
}
@@ -61,6 +64,11 @@ export type EventParams = {
6164
[EVENT_PARAM.COLUMN_NAME]: string;
6265
[EVENT_PARAM.SORT_DIRECTION]: SORT_DIRECTION;
6366
};
67+
[EVENT_NAME.FILE_DOWNLOADED]: {
68+
[EVENT_PARAM.ENTITY_NAME]: string;
69+
[EVENT_PARAM.RELATED_ENTITY_ID]: string;
70+
[EVENT_PARAM.RELATED_ENTITY_NAME]: string;
71+
};
6472
[EVENT_NAME.FILTER_SELECTED]: {
6573
[EVENT_PARAM.FILTER_NAME]: string;
6674
[EVENT_PARAM.FILTER_VALUE]: string;

src/components/Export/common/tracking.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EVENT_NAME, EVENT_PARAM } from "../../../common/analytics/entities";
66
* @param entity_name - Entity (tab) name.
77
* @param toolName - Tool name.
88
*/
9-
export function bulkDownloadTracking(
9+
export function trackBulkDownloadRequested(
1010
entity_name: string,
1111
toolName: string
1212
): void {
@@ -21,7 +21,7 @@ export function bulkDownloadTracking(
2121
* Executes event tracking for the file manifest export.
2222
* @param entity_name - Entity (tab) name.
2323
*/
24-
export function fileManifestTracking(entity_name: string): void {
24+
export function trackFileManifestRequested(entity_name: string): void {
2525
// Track the file manifest requested event.
2626
track(EVENT_NAME.INDEX_FILE_MANIFEST_REQUESTED, {
2727
[EVENT_PARAM.ENTITY_NAME]: entity_name,
@@ -32,9 +32,28 @@ export function fileManifestTracking(entity_name: string): void {
3232
* Executes event tracking for the Terra export.
3333
* @param entity_name - Entity (tab) name.
3434
*/
35-
export function exportToTerraTracking(entity_name: string): void {
35+
export function trackExportToTerraRequested(entity_name: string): void {
3636
// Track the export to terra event.
3737
track(EVENT_NAME.INDEX_ANALYZE_IN_TERRA_REQUESTED, {
3838
[EVENT_PARAM.ENTITY_NAME]: entity_name,
3939
});
4040
}
41+
42+
/**
43+
* Executes event tracking for individual file downloads
44+
* @param entity_name - The name of the file downloaded.
45+
* @param related_entity_id - The ID of the file's dataset / project
46+
* @param related_entity_name -The name of the file's dataset / project
47+
*/
48+
export function trackFileDownloaded(
49+
entity_name: string,
50+
related_entity_id: string,
51+
related_entity_name: string
52+
): void {
53+
// Track the file downloaded event.
54+
track(EVENT_NAME.FILE_DOWNLOADED, {
55+
[EVENT_PARAM.ENTITY_NAME]: entity_name,
56+
[EVENT_PARAM.RELATED_ENTITY_ID]: related_entity_id,
57+
[EVENT_PARAM.RELATED_ENTITY_NAME]: related_entity_name,
58+
});
59+
}

src/components/Export/components/DownloadCurlCommand/downloadCurlCommand.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
ExecutionEnvironment,
1313
FormFacet,
1414
} from "../../common/entities";
15-
import { bulkDownloadTracking } from "../../common/tracking";
15+
import { trackBulkDownloadRequested } from "../../common/tracking";
1616
import { DownloadCurlCommandNotStarted } from "./components/DownloadCurlCommandNotStarted/downloadCurlCommandNotStarted";
1717
import { DownloadCurlCommandReady } from "./components/DownloadCurlCommandReady/downloadCurlCommandReady";
1818

@@ -63,7 +63,7 @@ export const DownloadCurlCommand = ({
6363
isLoading={isLoading}
6464
onRequestManifest={(): void => {
6565
// Execute GTM tracking.
66-
bulkDownloadTracking(entityList, executionEnvironment);
66+
trackBulkDownloadRequested(entityList, executionEnvironment);
6767
// Request manifest.
6868
run();
6969
}}

src/components/Export/components/ExportToTerra/exportToTerra.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useFileManifest } from "../../../../hooks/useFileManifest/useFileManife
77
import { useRequestFileManifest } from "../../../../hooks/useFileManifest/useRequestFileManifest";
88
import { FileManifestState } from "../../../../providers/fileManifestState";
99
import { FormFacet, ManifestDownloadFormat } from "../../common/entities";
10-
import { exportToTerraTracking } from "../../common/tracking";
10+
import { trackExportToTerraRequested } from "../../common/tracking";
1111
import { ExportToTerraNotStarted } from "./components/ExportToTerraNotStarted/exportToTerraNotStarted";
1212
import { ExportToTerraReady } from "./components/ExportToTerraReady/exportToTerraReady";
1313

@@ -57,7 +57,7 @@ export const ExportToTerra = ({
5757
manifestDownloadFormats={manifestDownloadFormats}
5858
onRequestManifest={(): void => {
5959
// Execute GA tracking
60-
exportToTerraTracking(entityList);
60+
trackExportToTerraRequested(entityList);
6161
// Request manifest
6262
run();
6363
}}

src/components/Export/components/ManifestDownload/manifestDownload.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useRequestFileManifest } from "../../../../hooks/useFileManifest/useReq
88
import { FileLocation } from "../../../../hooks/useRequestFileLocation";
99
import { FileManifestState } from "../../../../providers/fileManifestState";
1010
import { FormFacet } from "../../common/entities";
11-
import { fileManifestTracking } from "../../common/tracking";
11+
import { trackFileManifestRequested } from "../../common/tracking";
1212
import { ManifestDownloadNotStarted } from "./components/ManifestDownloadNotStarted/manifestDownloadNotStarted";
1313
import { ManifestDownloadReady } from "./components/ManifestDownloadReady/manifestDownloadReady";
1414

@@ -55,7 +55,7 @@ export const ManifestDownload = ({
5555
formFacet={formFacet}
5656
isLoading={isLoading}
5757
onRequestManifest={(): void => {
58-
fileManifestTracking(entityList);
58+
trackFileManifestRequested(entityList);
5959
run();
6060
}}
6161
/>

src/components/Index/components/AzulFileDownload/azulFileDownload.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useFileLocation } from "../../../../hooks/useFileLocation";
44
import { DownloadIcon } from "../../../common/CustomIcon/components/DownloadIcon/downloadIcon";
55
import { LoadingIcon } from "../../../common/CustomIcon/components/LoadingIcon/loadingIcon";
66
import { IconButton } from "../../../common/IconButton/iconButton";
7+
import { trackFileDownloaded } from "../../../Export/common/tracking";
78
import { StyledIconButton } from "./azulFileDownload.styles";
89
import {
910
AZUL_FILE_DOWNLOAD_TEST_ID,
@@ -12,10 +13,16 @@ import {
1213
} from "./common/constants";
1314

1415
export interface AzulFileDownloadProps {
16+
entityName: string; // The name of the file downloaded.
17+
relatedEntityId: string; // An array of IDs of the file's datasets / projects
18+
relatedEntityName: string; // An array of names of the file's datasets / projects
1519
url?: string; // Original "file fetch URL" as returned from Azul endpoint.
1620
}
1721

1822
export const AzulFileDownload = ({
23+
entityName,
24+
relatedEntityId,
25+
relatedEntityName,
1926
url,
2027
}: AzulFileDownloadProps): JSX.Element => {
2128
const { fileUrl, isLoading, run } = useFileLocation(url);
@@ -49,6 +56,7 @@ export const AzulFileDownload = ({
4956
Icon={isLoading ? LoadingIcon : DownloadIcon}
5057
onClick={(): void => {
5158
setIsRequestPending(true);
59+
trackFileDownloaded(entityName, relatedEntityId, relatedEntityName);
5260
run();
5361
}}
5462
size="medium"

tests/azulFileDownload.test.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ describe("AzulFileDownload", () => {
2121
const FILE_URL = "https://example.com/storage/file";
2222
const MOCK_RUN = jest.fn();
2323
const URL = "https://example.com/repository/file";
24+
const TRACKING_PARAMETERS = {
25+
entityName: "filename.extension",
26+
relatedEntityId: "id",
27+
relatedEntityName: "name",
28+
};
2429
beforeEach(() => {
2530
(useFileLocation as jest.Mock).mockReturnValue({
2631
fileUrl: undefined,
@@ -33,24 +38,24 @@ describe("AzulFileDownload", () => {
3338
});
3439
describe("download button", () => {
3540
test("should render the download button", () => {
36-
render(<AzulFileDownload url={URL} />);
41+
render(<AzulFileDownload {...TRACKING_PARAMETERS} url={URL} />);
3742
const buttonEl = getButtonById(AZUL_FILE_REQUEST_DOWNLOAD_TEST_ID);
3843
expect(buttonEl).not.toBeNull();
3944
});
4045
test("should disable the download button if URL is undefined", () => {
41-
render(<AzulFileDownload />);
46+
render(<AzulFileDownload {...TRACKING_PARAMETERS} />);
4247
const buttonEl = getButtonById(AZUL_FILE_REQUEST_DOWNLOAD_TEST_ID);
4348
expect(buttonEl.disabled).toBe(true);
4449
});
4550
test("should enable the download button if a URL is provided", () => {
46-
render(<AzulFileDownload url={URL} />);
51+
render(<AzulFileDownload {...TRACKING_PARAMETERS} url={URL} />);
4752
const buttonEl = getButtonById(AZUL_FILE_REQUEST_DOWNLOAD_TEST_ID);
4853
expect(buttonEl.disabled).toBe(false);
4954
});
5055
});
5156
describe("download functionality", () => {
5257
test("should call the run function when the button is clicked", () => {
53-
render(<AzulFileDownload url={URL} />);
58+
render(<AzulFileDownload {...TRACKING_PARAMETERS} url={URL} />);
5459
const buttonEl = getButtonById(AZUL_FILE_REQUEST_DOWNLOAD_TEST_ID);
5560
fireEvent.click(buttonEl);
5661
expect(MOCK_RUN).toHaveBeenCalled();
@@ -61,7 +66,7 @@ describe("AzulFileDownload", () => {
6166
isLoading: false,
6267
run: MOCK_RUN,
6368
});
64-
render(<AzulFileDownload url={URL} />);
69+
render(<AzulFileDownload {...TRACKING_PARAMETERS} url={URL} />);
6570
const buttonEl = getButtonById(AZUL_FILE_REQUEST_DOWNLOAD_TEST_ID);
6671
fireEvent.click(buttonEl);
6772
await waitFor(() => {
@@ -80,7 +85,7 @@ describe("AzulFileDownload", () => {
8085
isLoading: false,
8186
run: MOCK_RUN,
8287
});
83-
render(<AzulFileDownload url={URL} />);
88+
render(<AzulFileDownload {...TRACKING_PARAMETERS} url={URL} />);
8489
const buttonEl = getButtonById(AZUL_FILE_REQUEST_DOWNLOAD_TEST_ID);
8590
const anchorEl = getAnchorEl(AZUL_FILE_DOWNLOAD_TEST_ID);
8691
fireEvent.click(buttonEl);

0 commit comments

Comments
 (0)