web/satellite/src/api: Absorb analytics errors

On more privacy-sensitive browsers, attempts to notify the satellite
about client-side events might be blocked, because the API endpoint on
the satellite contains the word "analytics".

We want to respect the privacy of these types of visitors, so rather
than changing the name of the endpoint to something else, this change
catches errors that are caused by attempting to use the analytics API,
and logs them to the console without interrupting the user's experience
of the website. These errors do not affect any essential behavior, so it
is okay if the user is not aware of them.

Change-Id: I6f49e0abcd64fd69802e4efa71a8d307cc5a3aee
This commit is contained in:
Moby von Briesen 2021-04-14 21:00:53 +02:00 committed by Maximillian von Briesen
parent 031206e453
commit 569942c04d

View File

@ -12,34 +12,48 @@ export class AnalyticsHttpApi {
private readonly ROOT_PATH: string = '/api/v0/analytics';
/**
* Used to get authentication token.
* Used to notify the satellite about arbitrary events that occur.
* Does not throw any errors so that expected UI behavior is not interrupted if the API call fails.
*
* @param eventName - name of the event
* @throws Error
*/
public async eventTriggered(eventName: string): Promise<void> {
const path = `${this.ROOT_PATH}/event`;
const body = {
eventName: eventName,
};
const response = await this.http.post(path, JSON.stringify(body));
if (response.ok) {
return;
try {
const path = `${this.ROOT_PATH}/event`;
const body = {
eventName: eventName,
};
const response = await this.http.post(path, JSON.stringify(body));
if (response.ok) {
return;
}
console.error("Attempted to notify Satellite that " + eventName + " occurred. Got bad response status code: " + response.status)
} catch(error) {
console.error("Could not notify satellite about " + eventName + " event occurrence (most likely blocked by browser).")
}
throw new Error('Can not track event');
}
/**
* Used to notify the satellite about arbitrary external link clicked events that occur.
* Does not throw any errors so that expected UI behavior is not interrupted if the API call fails.
*
* @param eventName - name of the event
* @param link - link that was clicked
*/
public async linkEventTriggered(eventName: string, link: string): Promise<void> {
const path = `${this.ROOT_PATH}/event`;
const body = {
eventName: eventName,
link: link,
};
const response = await this.http.post(path, JSON.stringify(body));
if (response.ok) {
return;
try {
const path = `${this.ROOT_PATH}/event`;
const body = {
eventName: eventName,
link: link,
};
const response = await this.http.post(path, JSON.stringify(body));
if (response.ok) {
return;
}
console.error("Attempted to notify Satellite that " + eventName + " occurred. Got bad response status code: " + response.status)
} catch(error) {
console.error("Could not notify satellite about " + eventName + " event occurrence (most likely blocked by browser).")
}
throw new Error('Can not track event');
}
}