storj/web/satellite/src/api/analytics.ts
prerna-parashar cc0518f473
satellite/analytics: Added segment.io page calls to track all the pages (#4880)
satellite/analytics: send analytics 'page visit' api requests when the user navigates around the UI
2022-06-09 11:54:23 -07:00

82 lines
3.2 KiB
TypeScript

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
import { HttpClient } from '@/utils/httpClient';
/**
* AnalyticsHttpApi is a console Analytics API.
* Exposes all analytics-related functionality
*/
export class AnalyticsHttpApi {
private readonly http: HttpClient = new HttpClient();
private readonly ROOT_PATH: string = '/api/v0/analytics';
/**
* 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
*/
public async eventTriggered(eventName: string): Promise<void> {
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).');
}
}
/**
* 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> {
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).');
}
}
/**
* Used to notify the satellite about arbitrary page visits that occur.
* Does not throw any errors so that expected UI behavior is not interrupted if the API call fails.
*
* @param pageName - name of the page
*/
public async pageVisit(pageName: string): Promise<void> {
try {
const path = `${this.ROOT_PATH}/page`;
const body = {
pageName: pageName,
};
const response = await this.http.post(path, JSON.stringify(body));
if (response.ok) {
return;
}
console.error('Attempted to notify Satellite that ' + pageName + ' occurred. Got bad response status code: ' + response.status);
} catch (error) {
console.error('Could not notify satellite about ' + pageName + ' event occurrence (most likely blocked by browser).');
}
}
}