From 08e046ea2a1e7fa1ff96167f4f852b6508142d77 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Fri, 24 Nov 2023 09:17:14 +0000 Subject: [PATCH] fix matomo bug --- .../app/components/about/about.component.html | 4 +- .../app/components/about/about.component.ts | 12 +++ .../src/app/services/enterprise.service.ts | 73 ++++++++++++++++--- 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/frontend/src/app/components/about/about.component.html b/frontend/src/app/components/about/about.component.html index a4cc51f90..cb45e9f19 100644 --- a/frontend/src/app/components/about/about.component.html +++ b/frontend/src/app/components/about/about.component.html @@ -36,14 +36,14 @@

If you're an individual...

- Become a Community Sponsor + Become a Community Sponsor

If you're a business...

- Become an Enterprise Sponsor + Become an Enterprise Sponsor diff --git a/frontend/src/app/components/about/about.component.ts b/frontend/src/app/components/about/about.component.ts index 8aa0422e8..026076f16 100644 --- a/frontend/src/app/components/about/about.component.ts +++ b/frontend/src/app/components/about/about.component.ts @@ -9,6 +9,7 @@ import { Router, ActivatedRoute } from '@angular/router'; import { map, share, tap } from 'rxjs/operators'; import { ITranslators } from '../../interfaces/node-api.interface'; import { DOCUMENT } from '@angular/common'; +import { EnterpriseService } from '../../services/enterprise.service'; @Component({ selector: 'app-about', @@ -33,6 +34,7 @@ export class AboutComponent implements OnInit { private websocketService: WebsocketService, private seoService: SeoService, public stateService: StateService, + private enterpriseService: EnterpriseService, private apiService: ApiService, private router: Router, private route: ActivatedRoute, @@ -108,4 +110,14 @@ export class AboutComponent implements OnInit { unmutePromoVideo(): void { this.promoVideo.nativeElement.muted = false; } + + onSponsorClick(e): boolean { + this.enterpriseService.goal(5); + return true; + } + + onEnterpriseClick(e): boolean { + this.enterpriseService.goal(6); + return true; + } } diff --git a/frontend/src/app/services/enterprise.service.ts b/frontend/src/app/services/enterprise.service.ts index bc80f337d..4ee3a0739 100644 --- a/frontend/src/app/services/enterprise.service.ts +++ b/frontend/src/app/services/enterprise.service.ts @@ -3,6 +3,7 @@ import { Inject, Injectable } from '@angular/core'; import { ApiService } from './api.service'; import { SeoService } from './seo.service'; import { StateService } from './state.service'; +import { ActivatedRoute } from '@angular/router'; @Injectable({ providedIn: 'root' @@ -11,12 +12,15 @@ export class EnterpriseService { exclusiveHostName = '.mempool.space'; subdomain: string | null = null; info: object = {}; + statsUrl: string; + siteId: number; constructor( @Inject(DOCUMENT) private document: Document, private apiService: ApiService, private seoService: SeoService, private stateService: StateService, + private activatedRoute: ActivatedRoute, ) { const subdomain = this.document.location.hostname.indexOf(this.exclusiveHostName) > -1 && this.document.location.hostname.split(this.exclusiveHostName)[0] || false; @@ -56,7 +60,7 @@ export class EnterpriseService { insertMatomo(siteId?: number): void { let statsUrl = '//stats.mempool.space/'; - + if (!siteId) { switch (this.document.location.hostname) { case 'mempool.space': @@ -88,16 +92,63 @@ export class EnterpriseService { } } + this.statsUrl = statsUrl; + this.siteId = siteId; + // @ts-ignore - const _paq = window._paq = window._paq || []; - _paq.push(['disableCookies']); - _paq.push(['trackPageView']); - _paq.push(['enableLinkTracking']); - (function() { - _paq.push(['setTrackerUrl', statsUrl+'m.php']); - _paq.push(['setSiteId', siteId.toString()]); - const d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; - g.type='text/javascript'; g.async=true; g.src=statsUrl+'m.js'; s.parentNode.insertBefore(g,s); - })(); + if (window._paq && window['Matomo']) { + window['Matomo'].addTracker(this.statsUrl+'m.php', this.siteId.toString()); + const matomo = this.getMatomo(); + matomo.setDocumentTitle(this.seoService.getTitle()); + matomo.setCustomUrl(this.getCustomUrl()); + matomo.disableCookies(); + matomo.trackPageView(); + matomo.enableLinkTracking(); + } else { + // @ts-ignore + const alreadyInitialized = !!window._paq; + // @ts-ignore + const _paq = window._paq = window._paq || []; + _paq.push(['setDocumentTitle', this.seoService.getTitle()]); + _paq.push(['setCustomUrl', this.getCustomUrl()]); + _paq.push(['disableCookies']); + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + if (alreadyInitialized) { + _paq.push(['addTracker', this.statsUrl+'m.php', this.siteId.toString()]); + } else { + (function() { + _paq.push(['setTrackerUrl', this.statsUrl+'m.php']); + _paq.push(['setSiteId', this.siteId.toString()]); + const d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; + // @ts-ignore + g.type='text/javascript'; g.async=true; g.src=statsUrl+'m.js'; s.parentNode.insertBefore(g,s); + })(); + } + } + } + + private getMatomo() { + if (this.siteId != null) { + return window['Matomo']?.getTracker(this.statsUrl, this.siteId); + } + } + + goal(id: number) { + // @ts-ignore + this.getMatomo()?.trackGoal(id); + } + + private getCustomUrl(): string { + let url = window.location.origin + '/'; + let route = this.activatedRoute; + while (route) { + const segment = route?.routeConfig?.path; + if (segment && segment.length) { + url += segment + '/'; + } + route = route.firstChild; + } + return url; } }