@@ -2,47 +2,36 @@ import { trackEvent } from "@canonical/analytics-events";
22
33const SEARCH_ID_KEY = "search_id" ;
44
5- let activeSearchId = "" ;
6-
7- function generateSearchId ( ) : string {
8- const id = crypto . randomUUID ( ) ;
9- sessionStorage . setItem ( SEARCH_ID_KEY , id ) ;
10- activeSearchId = id ;
11- return id ;
12- }
13-
14- function getOrCreateSearchId ( ) : string {
15- if ( activeSearchId ) return activeSearchId ;
16-
17- const stored = sessionStorage . getItem ( SEARCH_ID_KEY ) ;
18- if ( stored ) {
19- activeSearchId = stored ;
20- return stored ;
5+ export function getSearchId ( ) : string {
6+ let id = sessionStorage . getItem ( SEARCH_ID_KEY ) ;
7+ if ( ! id ) {
8+ id = crypto . randomUUID ( ) ;
9+ sessionStorage . setItem ( SEARCH_ID_KEY , id ) ;
2110 }
22-
23- return generateSearchId ( ) ;
11+ return id ;
2412}
2513
2614export function trackSearchSubmitted (
2715 source : "home" | "store" ,
2816 query : string ,
2917) : void {
30- const searchId = generateSearchId ( ) ;
31- const target =
18+ const searchId = crypto . randomUUID ( ) ;
19+ sessionStorage . setItem ( SEARCH_ID_KEY , searchId ) ;
20+
21+ trackEvent (
3222 source === "home"
3323 ? "snap_home_search_submitted"
34- : "snap_store_search_submitted" ;
35-
36- trackEvent ( target , { search_id : searchId , query } ) ;
24+ : "snap_store_search_submitted" ,
25+ { search_id : searchId , query } ,
26+ ) ;
3727}
3828
3929export function trackSearchResults (
4030 query : string ,
4131 totalItems : number ,
4232 page : number ,
4333) : void {
44- const searchId = getOrCreateSearchId ( ) ;
45- if ( ! searchId ) return ;
34+ const searchId = getSearchId ( ) ;
4635
4736 if ( totalItems > 0 ) {
4837 trackEvent ( "snap_store_search_results_loaded" , {
@@ -63,11 +52,8 @@ export function trackSearchResultClicked(
6352 query : string ,
6453 position : number ,
6554) : void {
66- const searchId = getOrCreateSearchId ( ) ;
67- if ( ! searchId ) return ;
68-
6955 trackEvent ( "snap_store_search_result_clicked" , {
70- search_id : searchId ,
56+ search_id : getSearchId ( ) ,
7157 query,
7258 position,
7359 } ) ;
0 commit comments