From 71aff239b80b80774761440577d7adacebaae7bf Mon Sep 17 00:00:00 2001 From: afrontend Date: Thu, 12 Nov 2026 23:31:50 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20URL=20=EB=A1=9C=20=EB=93=A4=EC=96=B4?= =?UTF-8?q?=EC=98=A8=20=EC=9E=90=EB=8F=99=20=EA=B2=80=EC=83=89=EB=8F=84=20?= =?UTF-8?q?search=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=EB=A1=9C=20=EC=B8=A1?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 북마클릿·공유 링크로 들어오면 첫 검색이 자동으로 실행되지만 trackSearch 를 부르지 않아 GA search 수에서 빠졌고 entry_ref=bm 도 page_view 에만 남았다. - 자동 첫 검색에서도 검색 버튼과 같은 범위 규칙(single/all/group)으로 search 이벤트를 보낸다 - 범위 계산을 trackSearchScope 로 모아 검색 버튼과 함께 쓴다 - 첫 검색 판단은 도서관 목록이 준비됐을 때 한 번만 한다. 제목 없이 열린 뒤 뒤로 가기로 제목이 생겼을 때 의존값이 바뀌면 검색이 한 번 더 실행될 수 있었다 - 뒤로 가기 재검색은 측정하지 않는다 --- src/hooks/useSearchManager.test.ts | 37 ++++++++++++++++++++++++++++++ src/hooks/useSearchManager.ts | 31 ++++++++++++++----------- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/hooks/useSearchManager.test.ts b/src/hooks/useSearchManager.test.ts index 7cf421b..e8b6652 100644 --- a/src/hooks/useSearchManager.test.ts +++ b/src/hooks/useSearchManager.test.ts @@ -387,5 +387,42 @@ describe("useSearchManager", () => { expect(trackSearch).toHaveBeenCalledWith("해리포터", "강남도서관", "single", 1); }); + + it("북마클릿으로 들어와 내 도서관을 자동 검색해도 group 으로 한 번 보낸다", () => { + vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY, ref: "bm" }); + + const { rerender } = renderHook(() => useSearchManager(createMockParams({ myLibraries: ["강남도서관"] }))); + rerender(); + + expect(trackSearch).toHaveBeenCalledTimes(1); + expect(trackSearch).toHaveBeenCalledWith("해리포터", DEFAULT_LIBRARY, "group", 1); + }); + + it("내 도서관이 없어 자동 검색하지 않으면 보내지 않는다", () => { + vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY, ref: "bm" }); + + renderHook(() => useSearchManager(createMockParams({ myLibraries: [] }))); + + expect(trackSearch).not.toHaveBeenCalled(); + }); + + it("공유 링크로 도서관과 함께 들어오면 single 로 보낸다", () => { + vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: "강남도서관" }); + + renderHook(() => useSearchManager(createMockParams())); + + expect(trackSearch).toHaveBeenCalledWith("해리포터", "강남도서관", "single", 1); + }); + + it("뒤로 가기로 다시 검색할 때는 보내지 않는다", () => { + renderHook(() => useSearchManager(createMockParams())); + vi.mocked(getUrlParams).mockReturnValue({ title: "해리포터", library: DEFAULT_LIBRARY }); + + act(() => { + window.dispatchEvent(new PopStateEvent("popstate")); + }); + + expect(trackSearch).not.toHaveBeenCalled(); + }); }); }); diff --git a/src/hooks/useSearchManager.ts b/src/hooks/useSearchManager.ts index 6663241..83b8bea 100644 --- a/src/hooks/useSearchManager.ts +++ b/src/hooks/useSearchManager.ts @@ -4,6 +4,17 @@ import { DEFAULT_LIBRARY } from "../constants"; import { getUrlParams, updateUrl } from "../utils/url"; import type { Library } from "../types"; +/** 검색 측정용 범위. 도서관 하나면 single, 전체면 all, 그 밖(필터·내 도서관)은 group */ +const searchScope = (libraryName: string, libraryCount: number, totalCount: number): SearchScope => + libraryName !== DEFAULT_LIBRARY ? "single" : libraryCount === totalCount ? "all" : "group"; + +/** 검색 이벤트를 보낸다. single 이면 도서관 수는 1 */ +const trackSearchScope = (title: string, libraryName: string, libraryCount: number, totalCount: number) => { + const scope = searchScope(libraryName, libraryCount, totalCount); + trackSearch(title, libraryName, scope, scope === "single" ? 1 : libraryCount); + return scope; +}; + export const useSearchManager = (params: { libraryNames: Library[]; filteredLibraries: Library[]; @@ -61,19 +72,24 @@ export const useSearchManager = (params: { useEffect(() => { if (libraryNames.length > 0 && !initialSearchFiredRef.current) { + // 첫 검색 판단은 도서관 목록이 준비됐을 때 한 번만 한다. + // 제목 없이 열린 뒤 뒤로 가기로 제목이 생겨도 여기서 다시 검색하지 않는다 (popstate 가 맡는다) + initialSearchFiredRef.current = true; const { title, library, ref } = getUrlParams(); if (title) { - initialSearchFiredRef.current = true; if (ref && library === DEFAULT_LIBRARY) { // 북마클릿·확장은 도서관을 모른다. 전체(1,342곳) 대신 내 도서관을 검색하고, // 내 도서관이 없으면 제목만 채워 두고 도서관을 고르게 한다. const mine = libraryNames.filter((lib) => myLibraries.includes(lib.name)); if (mine.length > 0) { setMyScope({ libraries: mine, libraryName: DEFAULT_LIBRARY, filteredLibraries }); + trackSearchScope(title, DEFAULT_LIBRARY, mine.length, libraryNames.length); performSearch(title, DEFAULT_LIBRARY, mine); } return; } + // URL 로 들어온 자동 검색(북마클릿·공유 링크)도 검색 버튼과 같이 측정한다 + trackSearchScope(title, library, libraryNames.length, libraryNames.length); performSearch(title, library, libraryNames); } } @@ -87,21 +103,10 @@ export const useSearchManager = (params: { resetFilters(); addToHistory(searchText); updateUrl(searchText, libraryName); - const scope: SearchScope = - libraryName !== DEFAULT_LIBRARY - ? "single" - : libraries.length === libraryNames.length - ? "all" - : "group"; + const scope = trackSearchScope(searchText, libraryName, libraries.length, libraryNames.length); if (scope === "single") { addToMyLibraries?.(libraryName); } - trackSearch( - searchText, - libraryName, - scope, - scope === "single" ? 1 : libraries.length, - ); performSearch(searchText, libraryName, libraries); }, [