Skip to content

Commit b3c46cd

Browse files
authored
[ENG-9037] feature(post metrics): post metrics endpoint to log user view of public project/registration (#534)
* feature(post metrics): post metrics endpoint to log user view of public project/registration * feature(post metrics): use project/preprint/registry datail page to call sendCountedUsage after resource is loaded and public * feature(post metrics): code updates * feature(post metrics): code updates * feature(post metrics): CR code updates
1 parent 099c037 commit b3c46cd

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

src/app/features/preprints/pages/preprint-details/preprint-details.component.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Component,
1414
computed,
1515
DestroyRef,
16+
effect,
1617
HostBinding,
1718
inject,
1819
OnDestroy,
@@ -50,6 +51,7 @@ import { CreateNewVersion, PreprintStepperSelectors } from '@osf/features/prepri
5051
import { IS_MEDIUM, pathJoin } from '@osf/shared/helpers';
5152
import { ReviewPermissions, UserPermissions } from '@shared/enums';
5253
import { CustomDialogService, MetaTagsService } from '@shared/services';
54+
import { AnalyticsService } from '@shared/services/analytics.service';
5355
import { DataciteService } from '@shared/services/datacite/datacite.service';
5456
import { ContributorsSelectors } from '@shared/stores';
5557

@@ -152,8 +154,16 @@ export class PreprintDetailsComponent implements OnInit, OnDestroy {
152154
return actions[0];
153155
});
154156

157+
private readonly analyticsService = inject(AnalyticsService);
158+
155159
constructor() {
156160
this.helpScoutService.setResourceType('preprint');
161+
effect(() => {
162+
const currentPreprint = this.preprint();
163+
if (currentPreprint && currentPreprint.isPublic) {
164+
this.analyticsService.sendCountedUsage(currentPreprint.id, 'preprint.detail').subscribe();
165+
}
166+
});
157167
}
158168

159169
private currentUserIsAdmin = computed(() => {

src/app/features/project/overview/project-overview.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
SubHeaderComponent,
5757
ViewOnlyLinkMessageComponent,
5858
} from '@shared/components';
59+
import { AnalyticsService } from '@shared/services/analytics.service';
5960
import { DataciteService } from '@shared/services/datacite/datacite.service';
6061

6162
import {
@@ -253,6 +254,8 @@ export class ProjectOverviewComponent implements OnInit {
253254
};
254255
});
255256

257+
readonly analyticsService = inject(AnalyticsService);
258+
256259
constructor() {
257260
this.setupCollectionsEffects();
258261
this.setupCleanup();
@@ -341,6 +344,12 @@ export class ProjectOverviewComponent implements OnInit {
341344
this.actions.getHomeWiki(ResourceType.Project, project.id);
342345
}
343346
});
347+
effect(() => {
348+
const currentProject = this.currentProject();
349+
if (currentProject && currentProject.isPublic) {
350+
this.analyticsService.sendCountedUsage(currentProject.id, 'project.detail').subscribe();
351+
}
352+
});
344353
}
345354

346355
private setupRouteChangeListener(): void {

src/app/features/registry/registry.component.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ActivatedRoute, RouterOutlet } from '@angular/router';
1010
import { ENVIRONMENT } from '@core/provider/environment.provider';
1111
import { ClearCurrentProvider } from '@core/store/provider';
1212
import { pathJoin } from '@osf/shared/helpers';
13-
import { MetaTagsService } from '@osf/shared/services';
13+
import { AnalyticsService, MetaTagsService } from '@osf/shared/services';
1414
import { DataciteService } from '@shared/services/datacite/datacite.service';
1515

1616
import { GetRegistryById, RegistryOverviewSelectors } from './store/registry-overview';
@@ -43,6 +43,7 @@ export class RegistryComponent implements OnDestroy {
4343
readonly registry = select(RegistryOverviewSelectors.getRegistry);
4444
readonly isRegistryLoading = select(RegistryOverviewSelectors.isRegistryLoading);
4545
readonly registry$ = toObservable(select(RegistryOverviewSelectors.getRegistry));
46+
readonly analyticsService = inject(AnalyticsService);
4647

4748
constructor() {
4849
effect(() => {
@@ -57,7 +58,15 @@ export class RegistryComponent implements OnDestroy {
5758
}
5859
});
5960

61+
effect(() => {
62+
const currentRegistry = this.registry();
63+
if (currentRegistry && currentRegistry.isPublic) {
64+
this.analyticsService.sendCountedUsage(currentRegistry.id, 'registry.detail').subscribe();
65+
}
66+
});
67+
6068
this.dataciteService.logIdentifiableView(this.registry$).pipe(takeUntilDestroyed(this.destroyRef)).subscribe();
69+
6170
}
6271

6372
ngOnDestroy(): void {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Observable } from 'rxjs';
2+
3+
import { inject, Injectable } from '@angular/core';
4+
5+
import { ENVIRONMENT } from '@core/provider/environment.provider';
6+
import { JsonApiService } from '@osf/shared/services';
7+
8+
@Injectable({ providedIn: 'root' })
9+
export class AnalyticsService {
10+
private readonly jsonApiService = inject(JsonApiService);
11+
private readonly environment = inject(ENVIRONMENT);
12+
13+
get apiDomainUrl() {
14+
return `${this.environment.apiDomainUrl}/_/metrics/events/counted_usage/`;
15+
}
16+
17+
sendCountedUsage(guid: string, routeName: string): Observable<void> {
18+
const payload = {
19+
data: {
20+
type: 'counted-usage',
21+
attributes: {
22+
item_guid: guid,
23+
action_labels: ['web', 'view'],
24+
pageview_info: {
25+
page_url: document.URL,
26+
page_title: document.title,
27+
referer_url: document.referrer,
28+
route_name: `angular-osf-web.${routeName}`,
29+
},
30+
},
31+
},
32+
};
33+
34+
return this.jsonApiService.post<void>(this.apiDomainUrl, payload);
35+
}
36+
}

src/app/shared/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './activity-logs';
22
export * from './addons';
3+
export { AnalyticsService } from './analytics.service';
34
export { BookmarksService } from './bookmarks.service';
45
export { BrandService } from './brand.service';
56
export { CollectionsService } from './collections.service';

0 commit comments

Comments
 (0)