Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UFAL/Matomo statistics using angulartics2 #748

Merged
merged 13 commits into from
Nov 29, 2024
Merged
24 changes: 1 addition & 23 deletions src/app/footer/footer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,7 @@ <h4><a href="https://lindat.cz/services">Services</a></h4>
ga('create', 'UA-27008245-2', 'cuni.cz');
ga('send', 'pageview');
//]]>
</script>
<!-- Piwik LINDAT/CLARIAH-CZ tracker -->
<script type="text/javascript">
//<![CDATA[
var _paq = _paq || [];
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
_paq.push(["setCookieDomain", "*.mff.cuni.cz"]);
_paq.push(["setDomains", ["*.mff.cuni.cz"]]);
_paq.push(['setCustomVariable', 1, "source", "common-theme", "page"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u='//lindat.mff.cuni.cz/piwik/';
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 2]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
//]]>
</script>
<noscript><p><img src="//lindat.mff.cuni.cz/piwik/piwik.php?idsite=2" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->
<!-- End TRACKING CODE -->
</script>
</footer>
</div>
</footer>
6 changes: 0 additions & 6 deletions src/app/footer/footer.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { CUSTOM_ELEMENTS_SCHEMA, DebugElement } from '@angular/core';

import { CommonModule } from '@angular/common';

import { By } from '@angular/platform-browser';

import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { StoreModule } from '@ngrx/store';

Expand Down Expand Up @@ -62,10 +60,6 @@ describe('Footer component', () => {
fixture = TestBed.createComponent(FooterComponent);

comp = fixture.componentInstance; // component test instance

// query for the title <p> by CSS element selector
de = fixture.debugElement.query(By.css('p'));
el = de.nativeElement;
});

it('should create footer', inject([FooterComponent], (app: FooterComponent) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class ViewTrackerComponent implements OnInit, OnDestroy {
action: 'page_view',
properties: {
object: this.object,
referrer
referrer,
category: 'page_view',
},
});
});
Expand Down
18 changes: 18 additions & 0 deletions src/main.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { environment } from './environments/environment';
import { AppConfig } from './config/app-config.interface';
import { extendEnvironmentWithAppConfig } from './config/config.util';
import { enableProdMode } from '@angular/core';
import { matomoSettings } from './matomo/matomo-settings';

const bootstrap = () => platformBrowserDynamic()
.bootstrapModule(BrowserAppModule, {});
Expand All @@ -26,6 +27,7 @@ const main = () => {
if (environment.production) {
enableProdMode();
}
addMatomoStatistics();

if (hasTransferState) {
// Configuration will be taken from transfer state during initialization
Expand All @@ -42,6 +44,22 @@ const main = () => {
}
};

function addMatomoStatistics() {
(window as any)._paq = (window as any)._paq || [];

// Push all configuration commands first
(window as any)._paq.push(['setTrackerUrl', matomoSettings.hostUrl + 'matomo.php']);
(window as any)._paq.push(['setSiteId', matomoSettings.siteId]);
(window as any)._paq.push(['enableLinkTracking']);

const g = document.createElement('script');
g.type = 'text/javascript';
g.async = true;
g.defer = true;
g.src = matomoSettings.hostUrl + 'matomo.js';
document.getElementsByTagName('head')[0].appendChild(g);
}

// support async tag or hmr
if (document.readyState === 'complete' && !hasTransferState) {
main();
Expand Down
7 changes: 7 additions & 0 deletions src/matomo/matomo-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Matomo settings for tracking statistics. This file could be mounted in the docker container.
*/
export const matomoSettings = {
hostUrl: 'http://localhost:8135/',
siteId: '1'
};
6 changes: 4 additions & 2 deletions src/modules/app/browser-app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { AppModule } from '../../app/app.module';
import { ClientCookieService } from '../../app/core/services/client-cookie.service';
import { CookieService } from '../../app/core/services/cookie.service';
import { AuthService } from '../../app/core/auth/auth.service';
import { Angulartics2GoogleTagManager, Angulartics2RouterlessModule } from 'angulartics2';
import {
Angulartics2GoogleTagManager, Angulartics2Module,
} from 'angulartics2';
import { SubmissionService } from '../../app/submission/submission.service';
import { StatisticsModule } from '../../app/statistics/statistics.module';
import { BrowserKlaroService } from '../../app/shared/cookies/browser-klaro.service';
Expand Down Expand Up @@ -54,7 +56,7 @@ export function getRequest(transferState: TransferState): any {
// forRoot ensures the providers are only created once
IdlePreloadModule.forRoot(),
StatisticsModule.forRoot(),
Angulartics2RouterlessModule.forRoot(),
Angulartics2Module.forRoot(),
BrowserAnimationsModule,
BrowserTransferStateModule,
TranslateModule.forRoot({
Expand Down
3 changes: 3 additions & 0 deletions src/modules/app/browser-init.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { MenuService } from '../../app/shared/menu/menu.service';
import { RootDataService } from '../../app/core/data/root-data.service';
import { firstValueFrom, Subscription } from 'rxjs';
import { ServerCheckGuard } from '../../app/core/server-check/server-check.guard';
import { Angulartics2Matomo } from 'angulartics2';

/**
* Performs client-side initialization.
Expand All @@ -59,6 +60,7 @@ export class BrowserInitService extends InitService {
protected menuService: MenuService,
private rootDataService: RootDataService,
protected serverCheckGuard: ServerCheckGuard,
private angulartics2Matomo: Angulartics2Matomo
) {
super(
store,
Expand Down Expand Up @@ -100,6 +102,7 @@ export class BrowserInitService extends InitService {
this.initRouteListeners();
this.themeService.listenForThemeChanges(true);
this.trackAuthTokenExpiration();
this.angulartics2Matomo.startTracking();

this.initKlaro();

Expand Down