Skip to content

Commit

Permalink
fix: ajout du tracking sur outil recherche cc (#6366)
Browse files Browse the repository at this point in the history
* fix: ajout du tracking sur outil recherche cc

* chore: review

* chore: review

---------

Co-authored-by: victor <[email protected]>
  • Loading branch information
Viczei and victor authored Dec 13, 2024
1 parent 26580e0 commit c1fd3d8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useState } from "react";
import { Autocomplete } from "../../common/Autocomplete/Autocomplete";
import { Agreement } from "../../../outils/types";
import { searchAgreement } from "../search";
import { useAgreementSearchTracking } from "../tracking";

type Props = {
onSearch?: (query: string, value?: Agreement[]) => void;
Expand All @@ -17,6 +18,7 @@ export const AgreementSearchInput = ({ onSearch }: Props) => {
"noSearch" | "lowSearch" | "notFoundSearch" | "errorSearch" | "fullSearch"
>("noSearch");
const [error, setError] = useState("");
const { emitAgreementSearchInputEvent } = useAgreementSearchTracking();
const getStateMessage = () => {
switch (searchState) {
case "lowSearch":
Expand Down Expand Up @@ -73,6 +75,9 @@ export const AgreementSearchInput = ({ onSearch }: Props) => {
}}
search={searchAgreement}
onSearch={(query, agreements) => {
if (query) {
emitAgreementSearchInputEvent(query);
}
if (onSearch) onSearch(query, agreements);
if (!query) {
setSearchState("noSearch");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import { ui } from "./ui";
import { wait } from "@testing-library/user-event/dist/utils";
import { act } from "react-dom/test-utils";
import { searchAgreement } from "../search";
import { sendEvent } from "../../utils";

jest.mock("../../utils", () => ({
sendEvent: jest.fn(),
}));

jest.mock("uuid", () => ({
v4: jest.fn(() => ""),
}));

jest.mock("../search", () => ({
searchAgreement: jest.fn(),
Expand Down Expand Up @@ -37,6 +46,12 @@ describe("Trouver sa CC - recherche par nom de CC", () => {
userAction = new UserAction();
userAction.setInput(ui.searchByName.input.get(), "16");
await wait();
expect(sendEvent).toHaveBeenCalledTimes(1);
expect(sendEvent).toHaveBeenCalledWith({
action: "Trouver sa convention collective",
category: "cc_search",
name: '{"query":"16"}',
});
expect(
ui.searchByName.autocompleteLines.IDCC16.name.query()
).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { v4 as generateUUID } from "uuid";
import { sendEvent } from "../utils";

export enum TrackingAgreementSearchEvent {
CC_SEARCH = "cc_search",
ENTERPRISE_SEARCH = "enterprise_search",
}

export const AGREEMENT_SEARCH_ACTION = "Trouver sa convention collective";

export const useAgreementSearchTracking = () => {
const emitAgreementSearchInputEvent = (query: string) => {
sendEvent({
category: TrackingAgreementSearchEvent.CC_SEARCH,
action: AGREEMENT_SEARCH_ACTION,
name: JSON.stringify({ query }),
value: generateUUID(),
});
};

return {
emitAgreementSearchInputEvent,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { searchEnterprises } from "../queries";
import { Enterprise } from "../types";
import { ApiGeoResult } from "../../Location/searchCities";
import { CardTitleStyle, ButtonStyle } from "../../convention-collective/style";
import { useEnterpriseAgreementSearchTracking } from "./tracking";

type Props = {
widgetMode?: boolean;
Expand All @@ -30,6 +31,9 @@ export const EnterpriseAgreementSearchInput = ({
const [searchState, setSearchState] = useState<
"noSearch" | "notFoundSearch" | "errorSearch" | "fullSearch" | "required"
>("noSearch");
const { emitEnterpriseAgreementSearchInputEvent } =
useEnterpriseAgreementSearchTracking();

const [search, setSearch] = useState<string | undefined>(defaultSearch);
const [loading, setLoading] = useState<boolean>(false);
const [location, setLocation] = useState<ApiGeoResult | undefined>(
Expand Down Expand Up @@ -85,6 +89,7 @@ export const EnterpriseAgreementSearchInput = ({
setSearchState("required");
return;
}
emitEnterpriseAgreementSearchInputEvent(search, location);
setLoading(true);
try {
const result = await searchEnterprises({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { EnterpriseAgreementSearch } from "../EnterpriseAgreementSearch";
import { ui } from "./ui";
import { wait } from "@testing-library/user-event/dist/utils";
import { searchEnterprises } from "../../queries";
import { sendEvent } from "../../../utils";

jest.mock("../../../utils", () => ({
sendEvent: jest.fn(),
}));

jest.mock("uuid", () => ({
v4: jest.fn(() => ""),
}));

jest.mock("../../queries", () => ({
searchEnterprises: jest.fn(),
Expand Down Expand Up @@ -58,6 +67,12 @@ describe("Trouver sa CC - recherche par nom d'entreprise CC", () => {
);
userAction.click(ui.enterpriseAgreementSearch.submitButton.get());
await wait();
expect(sendEvent).toHaveBeenCalledTimes(1);
expect(sendEvent).toHaveBeenCalledWith({
action: "Trouver sa convention collective",
category: "enterprise_search",
name: '{"query":"carrefour"}',
});
expect(
ui.enterpriseAgreementSearch.resultLines.carrefour.title.query()
).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { v4 as generateUUID } from "uuid";
import { sendEvent } from "../../utils";
import { ApiGeoResult } from "src/modules/Location/searchCities";
import {
AGREEMENT_SEARCH_ACTION,
TrackingAgreementSearchEvent,
} from "src/modules/convention-collective/tracking";

export const useEnterpriseAgreementSearchTracking = () => {
const emitEnterpriseAgreementSearchInputEvent = (
query: string,
apiGeoResult?: ApiGeoResult
) => {
sendEvent({
category: TrackingAgreementSearchEvent.ENTERPRISE_SEARCH,
action: AGREEMENT_SEARCH_ACTION,
name: JSON.stringify({ query, apiGeoResult }),
value: generateUUID(),
});
};

return {
emitEnterpriseAgreementSearchInputEvent,
};
};

0 comments on commit c1fd3d8

Please sign in to comment.