Skip to content

Commit a4e71c8

Browse files
committed
Add custom dimension (handle) to pageTrack of matomo
This extends/overrides the pageTrack/eventTrack methods of angulartics matomo plugin and hooks into the event tracking (view-tracker.component) that's there for DSpace internal statistics.
1 parent 654611d commit a4e71c8

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/app/statistics/angulartics/dspace/view-tracker.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export class ViewTrackerComponent implements OnInit, OnDestroy {
3636
this.sub = this.referrerService.getReferrer()
3737
.pipe(take(1))
3838
.subscribe((referrer: string) => {
39+
let dc_identifier = this.object.firstMetadataValue("dc.identifier.uri");
3940
this.angulartics2.eventTrack.next({
4041
action: 'page_view',
4142
properties: {
4243
object: this.object,
4344
referrer,
4445
category: 'page_view',
46+
dc_identifier: dc_identifier
4547
},
4648
});
4749
});

src/config/default-app-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ export class DefaultAppConfig implements AppConfig {
433433
// Matomo configuration
434434
matomo: MatomoConfig = {
435435
hostUrl: 'http://localhost:8135/',
436-
siteId: '1'
436+
siteId: '1',
437+
dimensionId: 1
437438
};
438439
}

src/config/matomo-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ export class MatomoConfig implements Config {
88
public hostUrl: string;
99

1010
public siteId: string;
11+
12+
public dimensionId: Number;
1113
}

src/modules/app/browser-init.service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ export class BrowserInitService extends InitService {
102102
this.initRouteListeners();
103103
this.themeService.listenForThemeChanges(true);
104104
this.trackAuthTokenExpiration();
105+
// ideally we'd add the custom dimension to the 'trackPageView' action only, but don't have that information
106+
// in pageTrack context. So we add it to page_view events, and remove it after the page view.
107+
// page_view events are fired via view-track.component, and exposes dc.identifier.uri via properties
108+
this.angulartics2Matomo.eventTrack = function (action, properties? : any) {
109+
if (action === 'page_view') {
110+
if (properties.dc_identifier) {
111+
(window as any)._paq.push(['setCustomDimension', environment.matomo.dimensionId, properties.dc_identifier]);
112+
}
113+
}
114+
};
115+
let pageTrack = this.angulartics2Matomo.pageTrack;
116+
this.angulartics2Matomo.pageTrack = function (path: string) {
117+
pageTrack.call(this, path);
118+
(window as any)._paq.push(['deleteCustomDimension', environment.matomo.dimensionId]);
119+
};
105120
this.angulartics2Matomo.startTracking();
106121

107122
this.initKlaro();

0 commit comments

Comments
 (0)