Skip to content

Commit

Permalink
feat: added file download tracking (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaten authored and NoopDog committed Nov 14, 2024
1 parent b6d3efa commit fb59db8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/analytics/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum EVENT_NAME {
ENTITY_SELECTED = "entity_selected",
ENTITY_TABLE_PAGINATED = "entity_table_paginated",
ENTITY_TABLE_SORTED = "entity_table_sorted",
FILE_DOWNLOADED = "file_downloaded",
FILTER_SELECTED = "filter_selected",
INDEX_ANALYZE_IN_TERRA_REQUESTED = "index_analyze_in_terra_requested",
INDEX_FILE_MANIFEST_REQUESTED = "index_file_manifest_requested",
Expand All @@ -23,6 +24,8 @@ export enum EVENT_PARAM {
FILTER_NAME = "filter_name",
FILTER_VALUE = "filter_value",
PAGINATION_DIRECTION = "pagination_direction",
RELATED_ENTITY_ID = "related_entity_id",
RELATED_ENTITY_NAME = "related_entity_name",
SORT_DIRECTION = "sort_direction",
TOOL_NAME = "tool_name",
}
Expand Down Expand Up @@ -61,6 +64,11 @@ export type EventParams = {
[EVENT_PARAM.COLUMN_NAME]: string;
[EVENT_PARAM.SORT_DIRECTION]: SORT_DIRECTION;
};
[EVENT_NAME.FILE_DOWNLOADED]: {
[EVENT_PARAM.ENTITY_NAME]: string;
[EVENT_PARAM.RELATED_ENTITY_ID]: string;
[EVENT_PARAM.RELATED_ENTITY_NAME]: string;
};
[EVENT_NAME.FILTER_SELECTED]: {
[EVENT_PARAM.FILTER_NAME]: string;
[EVENT_PARAM.FILTER_VALUE]: string;
Expand Down
19 changes: 19 additions & 0 deletions src/components/Export/common/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,22 @@ export function exportToTerraTracking(entity_name: string): void {
[EVENT_PARAM.ENTITY_NAME]: entity_name,
});
}

/**
* Executes event tracking for individual file downloads
* @param entity_name - The name of the file downloaded.
* @param related_entity_id - The ID of the file's dataset / project
* @param related_entity_name -The name of the file's dataset / project
*/
export function trackFileDownload(
entity_name: string,
related_entity_id: string,
related_entity_name: string
): void {
// Track the file downloaded event.
track(EVENT_NAME.FILE_DOWNLOADED, {
[EVENT_PARAM.ENTITY_NAME]: entity_name,
[EVENT_PARAM.RELATED_ENTITY_ID]: related_entity_id,
[EVENT_PARAM.RELATED_ENTITY_NAME]: related_entity_name,
});
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Box } from "@mui/material";
import React, { Fragment, useEffect, useRef, useState } from "react";
import { stringifyValues } from "../../../../common/utils";
import { useFileLocation } from "../../../../hooks/useFileLocation";
import { DownloadIcon } from "../../../common/CustomIcon/components/DownloadIcon/downloadIcon";
import { LoadingIcon } from "../../../common/CustomIcon/components/LoadingIcon/loadingIcon";
import { IconButton } from "../../../common/IconButton/iconButton";
import { trackFileDownload } from "../../../Export/common/tracking";
import { StyledIconButton } from "./azulFileDownload.styles";
import {
AZUL_FILE_DOWNLOAD_TEST_ID,
Expand All @@ -12,10 +14,16 @@ import {
} from "./common/constants";

export interface AzulFileDownloadProps {
entityName?: string; // The name of the file downloaded.
relatedEntityId?: string[]; // An array of IDs of the file's datasets / projects
relatedEntityName?: string[]; // An array of names of the file's datasets / projects
url?: string; // Original "file fetch URL" as returned from Azul endpoint.
}

export const AzulFileDownload = ({
entityName,
relatedEntityId,
relatedEntityName,
url,
}: AzulFileDownloadProps): JSX.Element => {
const { fileUrl, isLoading, run } = useFileLocation(url);
Expand Down Expand Up @@ -49,6 +57,11 @@ export const AzulFileDownload = ({
Icon={isLoading ? LoadingIcon : DownloadIcon}
onClick={(): void => {
setIsRequestPending(true);
trackFileDownload(
entityName ?? "",
stringifyValues(relatedEntityId ?? [""]),
stringifyValues(relatedEntityName ?? [""])
);
run();
}}
size="medium"
Expand Down

0 comments on commit fb59db8

Please sign in to comment.