Skip to content

Commit

Permalink
task/WG-191: do not use headers for analytics information due to WG-1…
Browse files Browse the repository at this point in the history
…91 (#180)

* Disable header and use query params are single route instead

Headers can't be used due to https://tacc-main.atlassian.net/browse/WG-191

* Fix linting errors

* Remove header-related code

* Fix typo
  • Loading branch information
nathanfranklin authored Dec 15, 2023
1 parent 6313b7f commit 8eb641e
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions angular/src/app/app.interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,34 @@ export class JwtInterceptor implements HttpInterceptor {
});
}

// for guest users, add a custom header with a unique id
if (!this.authSvc.isLoggedIn()) {
// Get (or create of needed) the guestUserID in local storage
let guestUuid = localStorage.getItem('guestUuid');

if (!guestUuid) {
guestUuid = uuidv4();
localStorage.setItem('guestUuid', guestUuid);
}
request = request.clone({
setHeaders: {
'X-Guest-UUID': guestUuid,
},
});
}

if (request.url.indexOf(this.envService.apiUrl) > -1) {
// Add information about what app is making the request
request = request.clone({
setHeaders: {
'X-Geoapi-Application': 'hazmapper',
},
});

// Using query params instead of custom headers due to https://tacc-main.atlassian.net/browse/WG-191
let analytics_params = {};

analytics_params = { ...analytics_params, application: 'hazmapper' };

// for guest users, add a unique id
if (!this.authSvc.isLoggedIn()) {
// Get (or create if needed) the guestUserID in local storage
let guestUuid = localStorage.getItem('guestUuid');

if (!guestUuid) {
guestUuid = uuidv4();
localStorage.setItem('guestUuid', guestUuid);
}

analytics_params = { ...analytics_params, guest_uuid: guestUuid };
}
/* Send analytics-related params to projects endpoint only (until we use headers
again in https://tacc-main.atlassian.net/browse/WG-192) */
if (this.isProjectFeaturesRequest(request)) {
// Clone the request and add query parameters
request = request.clone({
setParams: { ...request.params, ...analytics_params },
});
}
}

if (
Expand Down Expand Up @@ -89,6 +94,17 @@ export class JwtInterceptor implements HttpInterceptor {

return next.handle(request);
}

/**
* Determines whether a given HTTP request targets the features endpoint of a project or public project.
*
* This endpoint is being logged with extra information for analytics purposes.
*/
private isProjectFeaturesRequest(request: HttpRequest<any>): boolean {
// Regular expression to match /projects/{projectId}/features or /public-projects/{projectId}/features
const urlPattern = /\/(projects|public-projects)\/\d+\/features\/?(?:\?.*)?/;
return request.method === 'GET' && urlPattern.test(request.url);
}
}

@Injectable()
Expand Down

0 comments on commit 8eb641e

Please sign in to comment.