Skip to content

Commit d8b6c8f

Browse files
authored
fix: check if AWSMA exists before tracking metric (#7754)
* fix: check if AWSMA exists before tracking metric * remove unused trackWhatsNew
1 parent 1b86184 commit d8b6c8f

File tree

1 file changed

+28
-95
lines changed

1 file changed

+28
-95
lines changed

src/utils/track.ts

Lines changed: 28 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ export const trackPageVisit = (): void => {
3535
}
3636
};
3737

38-
export const trackPageFetchException = (): void => {
39-
if (typeof window !== 'undefined' && typeof s != 'undefined') {
40-
s.linkTrackVars =
41-
'prop39,prop41,prop50,prop61,prop62,eVar39,eVar41,eVar50,eVar61,eVar62,eVar69';
42-
s.tl(true, 'o', 'page fetch exception');
43-
}
44-
};
45-
4638
export const trackExternalLink = (hrefTo: string): void => {
4739
if (typeof window !== 'undefined' && typeof s != 'undefined') {
4840
s.linkTrackVars =
@@ -51,57 +43,6 @@ export const trackExternalLink = (hrefTo: string): void => {
5143
}
5244
};
5345

54-
export const setSearchQuery = (query: string): void => {
55-
if (typeof window !== 'undefined' && typeof s != 'undefined') {
56-
s.eVar26 = query;
57-
}
58-
};
59-
60-
const triggerNoSearchResults = (query: string): void => {
61-
const queryBackup: string = s.eVar26;
62-
const resultCountBackup: number = parseInt(s.eVar27, 10);
63-
64-
s.eVar26 = query;
65-
s.eVar27 = '0'; // If it's the number 0, the variable won't be sent
66-
s.linkTrackVars =
67-
'prop39,prop41,prop50,prop61,prop62,eVar26,eVar27,eVar39,eVar41,eVar50,eVar61,eVar62,eVar69,events';
68-
s.linkTrackEvents = 'event2';
69-
s.events = 'event2';
70-
s.tl(true, 'o', 'internal search');
71-
72-
s.eVar26 = queryBackup;
73-
s.eVar27 = resultCountBackup.toString();
74-
};
75-
76-
let noResultsTimeout: NodeJS.Timeout;
77-
export const setSearchResultCount = (resultCount: number): void => {
78-
if (typeof window !== 'undefined' && typeof s != 'undefined') {
79-
s.eVar27 = resultCount.toString();
80-
s.events = resultCount === 0 ? 'event1' : 'event2';
81-
82-
if (resultCount === 0) {
83-
if (noResultsTimeout) {
84-
clearTimeout(noResultsTimeout);
85-
}
86-
noResultsTimeout = setTimeout(
87-
triggerNoSearchResults.bind(null, s.eVar26),
88-
1000
89-
);
90-
}
91-
}
92-
};
93-
94-
export const trackSearchQuery = (_input, _event, suggestion): void => {
95-
if (typeof window !== 'undefined' && typeof s != 'undefined') {
96-
s.linkTrackVars =
97-
'prop39,prop41,prop50,prop61,prop62,eVar26,eVar27,eVar39,eVar41,eVar50,eVar61,eVar62,eVar69,events';
98-
s.linkTrackEvents = 'event1';
99-
s.events = 'event1';
100-
s.tl(true, 'o', 'internal search');
101-
}
102-
window.location.assign(suggestion.url);
103-
};
104-
10546
export const trackFeedbackSubmission = (feedback: boolean) => {
10647
const opt = {
10748
event: {
@@ -110,11 +51,13 @@ export const trackFeedbackSubmission = (feedback: boolean) => {
11051
}
11152
};
11253

113-
AWSMA.ready(() => {
114-
document.dispatchEvent(
115-
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
116-
);
117-
});
54+
if (AWSMA) {
55+
AWSMA.ready(() => {
56+
document.dispatchEvent(
57+
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
58+
);
59+
});
60+
}
11861
};
11962

12063
export const trackExpanderOpen = (expanderId) => {
@@ -125,11 +68,13 @@ export const trackExpanderOpen = (expanderId) => {
12568
}
12669
};
12770

128-
AWSMA.ready(() => {
129-
document.dispatchEvent(
130-
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
131-
);
132-
});
71+
if (AWSMA) {
72+
AWSMA.ready(() => {
73+
document.dispatchEvent(
74+
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
75+
);
76+
});
77+
}
13378
};
13479

13580
export const trackCopyClicks = (data) => {
@@ -141,27 +86,13 @@ export const trackCopyClicks = (data) => {
14186
data: data
14287
};
14388

144-
AWSMA.ready(() => {
145-
document.dispatchEvent(
146-
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
147-
);
148-
});
149-
};
150-
151-
// Track the click on the "Whats new" banner component
152-
export const trackWhatsNewBanner = () => {
153-
const opt = {
154-
event: {
155-
type: 'click',
156-
name: 'WhatsNewBanner'
157-
}
158-
};
159-
160-
AWSMA.ready(() => {
161-
document.dispatchEvent(
162-
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
163-
);
164-
});
89+
if (AWSMA) {
90+
AWSMA.ready(() => {
91+
document.dispatchEvent(
92+
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
93+
);
94+
});
95+
}
16596
};
16697

16798
// Track the click on the Version Switcher component
@@ -173,9 +104,11 @@ export const trackVersionChange = (viewOld: boolean) => {
173104
}
174105
};
175106

176-
AWSMA.ready(() => {
177-
document.dispatchEvent(
178-
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
179-
);
180-
});
107+
if (AWSMA) {
108+
AWSMA.ready(() => {
109+
document.dispatchEvent(
110+
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
111+
);
112+
});
113+
}
181114
};

0 commit comments

Comments
 (0)