Skip to content

fix: check if AWSMA exists before tracking metric #7754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 35 additions & 84 deletions src/utils/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ export const trackPageVisit = (): void => {
}
};

export const trackPageFetchException = (): void => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in use

if (typeof window !== 'undefined' && typeof s != 'undefined') {
s.linkTrackVars =
'prop39,prop41,prop50,prop61,prop62,eVar39,eVar41,eVar50,eVar61,eVar62,eVar69';
s.tl(true, 'o', 'page fetch exception');
}
};

export const trackExternalLink = (hrefTo: string): void => {
if (typeof window !== 'undefined' && typeof s != 'undefined') {
s.linkTrackVars =
Expand All @@ -51,57 +43,6 @@ export const trackExternalLink = (hrefTo: string): void => {
}
};

export const setSearchQuery = (query: string): void => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not in use

if (typeof window !== 'undefined' && typeof s != 'undefined') {
s.eVar26 = query;
}
};

const triggerNoSearchResults = (query: string): void => {
const queryBackup: string = s.eVar26;
const resultCountBackup: number = parseInt(s.eVar27, 10);

s.eVar26 = query;
s.eVar27 = '0'; // If it's the number 0, the variable won't be sent
s.linkTrackVars =
'prop39,prop41,prop50,prop61,prop62,eVar26,eVar27,eVar39,eVar41,eVar50,eVar61,eVar62,eVar69,events';
s.linkTrackEvents = 'event2';
s.events = 'event2';
s.tl(true, 'o', 'internal search');

s.eVar26 = queryBackup;
s.eVar27 = resultCountBackup.toString();
};

let noResultsTimeout: NodeJS.Timeout;
export const setSearchResultCount = (resultCount: number): void => {
if (typeof window !== 'undefined' && typeof s != 'undefined') {
s.eVar27 = resultCount.toString();
s.events = resultCount === 0 ? 'event1' : 'event2';

if (resultCount === 0) {
if (noResultsTimeout) {
clearTimeout(noResultsTimeout);
}
noResultsTimeout = setTimeout(
triggerNoSearchResults.bind(null, s.eVar26),
1000
);
}
}
};

export const trackSearchQuery = (_input, _event, suggestion): void => {
if (typeof window !== 'undefined' && typeof s != 'undefined') {
s.linkTrackVars =
'prop39,prop41,prop50,prop61,prop62,eVar26,eVar27,eVar39,eVar41,eVar50,eVar61,eVar62,eVar69,events';
s.linkTrackEvents = 'event1';
s.events = 'event1';
s.tl(true, 'o', 'internal search');
}
window.location.assign(suggestion.url);
};

export const trackFeedbackSubmission = (feedback: boolean) => {
const opt = {
event: {
Expand All @@ -110,11 +51,13 @@ export const trackFeedbackSubmission = (feedback: boolean) => {
}
};

AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
if (AWSMA) {
AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
}
};

export const trackExpanderOpen = (expanderId) => {
Expand All @@ -125,11 +68,13 @@ export const trackExpanderOpen = (expanderId) => {
}
};

AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
if (AWSMA) {
AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
}
};

export const trackCopyClicks = (data) => {
Expand All @@ -141,11 +86,13 @@ export const trackCopyClicks = (data) => {
data: data
};

AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
if (AWSMA) {
AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
}
};

// Track the click on the "Whats new" banner component
Expand All @@ -157,11 +104,13 @@ export const trackWhatsNewBanner = () => {
}
};

AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
if (AWSMA) {
AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
}
};

// Track the click on the Version Switcher component
Expand All @@ -173,9 +122,11 @@ export const trackVersionChange = (viewOld: boolean) => {
}
};

AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
if (AWSMA) {
AWSMA.ready(() => {
document.dispatchEvent(
new CustomEvent(AWSMA.TRIGGER_EVENT, { detail: opt })
);
});
}
};