From eb7245e4e1c26bfaf57f7eb2d10104439f8b13d5 Mon Sep 17 00:00:00 2001 From: ncastejon <castejon.nicolas@gmail.com> Date: Mon, 30 Jul 2018 09:15:55 +0200 Subject: [PATCH] Variabilize the matomo id and url servers inside the docker integration --- .gitlab-ci.yml | 4 ++ docker-compose.yml | 7 +++- nginx.conf => nginx.conf.template | 19 ++++++++++ src/app/app.component.spec.ts | 3 ++ src/app/core/models/index.ts | 2 + src/app/core/models/matomo.model.ts | 3 ++ src/app/core/services/index.ts | 3 ++ src/app/core/services/matomo.service.ts | 38 +++++++++++++++++++ .../dataset-info.component.spec.ts | 1 + .../dataset-list/dataset-list.component.html | 4 +- .../dataset-list/dataset-list.component.ts | 18 ++++++--- src/app/geosource/models/metadata.model.ts | 1 + src/environments/environment.prod.ts | 4 ++ src/environments/environment.ts | 4 ++ src/i18n/error-messages/error-messages.fr.ts | 3 ++ src/i18n/error-messages/error-messages.ts | 3 ++ src/index.html | 13 ------- 17 files changed, 108 insertions(+), 22 deletions(-) rename nginx.conf => nginx.conf.template (59%) create mode 100644 src/app/core/models/matomo.model.ts create mode 100644 src/app/core/services/matomo.service.ts diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bfe43878..9b7b4f44 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -28,6 +28,8 @@ deploy_development: - sandbox script: - export NGINX_PORT=8081 + - export MATOMO_SITE_ID=1 + - export MATOMO_SERVER_URL=https://matomo-intothesky.alpha.grandlyon.com - docker-compose --project-name data-reloaded-dev up -d environment: name: development @@ -47,6 +49,8 @@ deploy_staging: - staging script: - export NGINX_PORT=8080 + - export MATOMO_SITE_ID=3 + - export MATOMO_SERVER_URL=https://matomo-intothesky.alpha.grandlyon.com - docker-compose --project-name data-reloaded-rec up -d environment: name: staging diff --git a/docker-compose.yml b/docker-compose.yml index cf0b085e..26019ee2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,12 @@ services: build: context: ./ volumes: - - ./nginx.conf:/etc/nginx/conf.d/default.conf + - ./nginx.conf.template:/etc/nginx/nginx.conf.template ports: - ${NGINX_PORT}:8080 + command: > + /bin/sh -c + 'echo $MATOMO_SITE_ID && sed "s#<MATOMO_SITE_ID>#$MATOMO_SITE_ID#g; s#<MATOMO_SERVER_URL>#$MATOMO_SERVER_URL#g" /etc/nginx/nginx.conf.template + > /etc/nginx/conf.d/default.conf + && nginx -g "daemon off;"' \ No newline at end of file diff --git a/nginx.conf b/nginx.conf.template similarity index 59% rename from nginx.conf rename to nginx.conf.template index 5bc65c45..5d81396b 100644 --- a/nginx.conf +++ b/nginx.conf.template @@ -4,6 +4,25 @@ server { server_name _; root /usr/share/nginx/html/; + set $matomo_script + "<script type='text/javascript'> + var _paq = _paq || []; + var siteId = <MATOMO_SITE_ID>; + window['siteId'] = siteId; + + _paq.push(['enableLinkTracking']); + (function() { + var u='<MATOMO_SERVER_URL>/'; + + _paq.push(['setTrackerUrl', u+'piwik.php']); + _paq.push(['setSiteId', siteId]); + 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>"; + + sub_filter '</head>' '$matomo_script</head>'; + # rule redirecting to fr or en regarding the browser preferences location = / { rewrite_by_lua ' diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index 30764061..8a4552f5 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -2,6 +2,8 @@ import { TestBed, async } from '@angular/core/testing'; import { AppComponent } from './app.component'; import { MockComponent } from 'ng2-mock-component'; import { RouterTestingModule } from '../../node_modules/@angular/router/testing'; +import { Angulartics2Module } from 'angulartics2'; +import { Angulartics2Piwik } from 'angulartics2/piwik'; describe('AppComponent', () => { beforeEach(async(() => { @@ -15,6 +17,7 @@ describe('AppComponent', () => { ], imports: [ RouterTestingModule, + Angulartics2Module.forRoot([Angulartics2Piwik]), ], }).compileComponents(); })); diff --git a/src/app/core/models/index.ts b/src/app/core/models/index.ts index 6e8ccb78..804ec59d 100644 --- a/src/app/core/models/index.ts +++ b/src/app/core/models/index.ts @@ -1,3 +1,5 @@ import { Notification, INotification } from './notification.model'; +import { IMatomoResponse } from './matomo.model'; export { Notification, INotification }; +export { IMatomoResponse }; diff --git a/src/app/core/models/matomo.model.ts b/src/app/core/models/matomo.model.ts new file mode 100644 index 00000000..317df8c8 --- /dev/null +++ b/src/app/core/models/matomo.model.ts @@ -0,0 +1,3 @@ +export interface IMatomoResponse { + nb_visits: number; +} diff --git a/src/app/core/services/index.ts b/src/app/core/services/index.ts index 28e31c67..5b2ff7e0 100644 --- a/src/app/core/services/index.ts +++ b/src/app/core/services/index.ts @@ -1,11 +1,14 @@ import { ErrorService } from './error.service'; import { NotificationService } from './notification.service'; +import { MatomoService } from './matomo.service'; export { ErrorService }; export { NotificationService }; +export { MatomoService }; // tslint:disable-next-line:variable-name export const CoreServices = [ ErrorService, NotificationService, + MatomoService, ]; diff --git a/src/app/core/services/matomo.service.ts b/src/app/core/services/matomo.service.ts new file mode 100644 index 00000000..6a4ee86b --- /dev/null +++ b/src/app/core/services/matomo.service.ts @@ -0,0 +1,38 @@ +import { Injectable } from '@angular/core'; +import { IMatomoResponse } from '../models/matomo.model'; +import { Observable } from 'rxjs'; +import { HttpClient } from '@angular/common/http'; +import { map, catchError } from 'rxjs/operators'; +import { environment } from '../../../environments/environment'; +import { ErrorService } from './error.service'; +import { errors } from '../../../i18n/error-messages/error-messages'; + +@Injectable() +export class MatomoService { + + constructor( + private http: HttpClient, + private _errorService: ErrorService, + ) { } + + getPageMetadataPageMetrics(metadataId: string): Observable<number> { + const dateNowString = new Date().toISOString().slice(0, 10); + const dateOneMonthAgo = new Date(); + dateOneMonthAgo.setMonth(new Date().getMonth() - 1); + const dateOneMonthAgoString = dateOneMonthAgo.toISOString().slice(0, 10); + + const url = environment.matomo.url + '/?module=API&method=Actions.getPageUrls' + + '&idSite=' + window['siteId'] + '&format=json&period=range&date=' + dateOneMonthAgoString + + ',' + dateNowString + + '&segment=pageUrl=@' + metadataId + '/info'; + return this.http.get<IMatomoResponse[]>(url).pipe( + map((results) => { + return results.length > 0 ? results.reduce((a, b) => a + b.nb_visits, 0) : 0; + }), + catchError( + (err) => { + throw this._errorService.handleError(err, { message: errors.matomo.getViews }); + }, + )); + } +} diff --git a/src/app/geosource/components/dataset-detail/dataset-info/dataset-info.component.spec.ts b/src/app/geosource/components/dataset-detail/dataset-info/dataset-info.component.spec.ts index 8cbf3479..75020799 100644 --- a/src/app/geosource/components/dataset-detail/dataset-info/dataset-info.component.spec.ts +++ b/src/app/geosource/components/dataset-detail/dataset-info/dataset-info.component.spec.ts @@ -56,6 +56,7 @@ const metadata = { max_north: 4, updateFrequency: '', max_west: 4, + nbViews: 10, crs: '', image: { url: '', diff --git a/src/app/geosource/components/dataset-list/dataset-list.component.html b/src/app/geosource/components/dataset-list/dataset-list.component.html index 60f7b399..55595e80 100644 --- a/src/app/geosource/components/dataset-list/dataset-list.component.html +++ b/src/app/geosource/components/dataset-list/dataset-list.component.html @@ -78,11 +78,11 @@ </span> {{ formatDate(dataset.metadata.geonet.changeDate,'dd MMMM yyyy')}} </p> - <p> + <p *ngIf="dataset.metadata.nbViews"> <span class="icon"> <i class="far fa-eye"></i> </span> - {{ dataset.metadata.popularity || '-' }} + {{ dataset.metadata.nbViews || '-' }} </p> </div> </div> diff --git a/src/app/geosource/components/dataset-list/dataset-list.component.ts b/src/app/geosource/components/dataset-list/dataset-list.component.ts index 19665650..28e49dbd 100644 --- a/src/app/geosource/components/dataset-list/dataset-list.component.ts +++ b/src/app/geosource/components/dataset-list/dataset-list.component.ts @@ -10,6 +10,7 @@ import { Notification } from '../../../core/models'; import { DatePipe } from '../../../../../node_modules/@angular/common'; import { Angulartics2 } from 'angulartics2'; import { HttpClient } from '@angular/common/http'; +import { MatomoService } from '../../../core/services/matomo.service'; @Component({ selector: 'app-dataset-list', @@ -34,7 +35,7 @@ export class DatasetListComponent implements OnInit, OnDestroy { private _router: Router, private _notificationService: NotificationService, private _datePipe: DatePipe, - private _http: HttpClient, + private _matomoService: MatomoService, ) { } ngOnInit() { @@ -73,11 +74,16 @@ export class DatasetListComponent implements OnInit, OnDestroy { this.loading = false; window.scrollTo(0, yPageOffset); - // const url = 'http://localhost:8181/piwik.php?search=' - // + this._datasetResearchService.searchString - // + '&idsite=1&rec=1'; - // console.log(url); - // this._http.get(url).subscribe(); + // Get the page views for each dataset + this.datasetList.forEach((dataset) => { + this._matomoService.getPageMetadataPageMetrics(dataset.metadata.geonet.uuid).subscribe( + (views) => { + dataset.metadata.nbViews = views; + }, + (err) => { + }); + }); + }, (err) => { this._notificationService.notify(new Notification({ diff --git a/src/app/geosource/models/metadata.model.ts b/src/app/geosource/models/metadata.model.ts index a49207b9..0640fa6d 100644 --- a/src/app/geosource/models/metadata.model.ts +++ b/src/app/geosource/models/metadata.model.ts @@ -131,6 +131,7 @@ export class Metadata { url: string; type: string; }; + 'nbViews': number; constructor(data?: IMetadata) { if (data) { diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index 9881b180..41077857 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -15,6 +15,10 @@ export const environment = { meta: servicesProxyUrl + '/elasticsearch/*.meta', }, + matomo: { + url: 'https://matomo-intothesky.alpha.grandlyon.com', + }, + // Path to the built app in a particular language angularAppHost: { fr: '/fr', diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 7db9e6c3..57910e66 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -15,6 +15,10 @@ export const environment = { meta: servicesProxyUrl + '/elasticsearch/*.meta', }, + matomo: { + url: 'https://matomo-intothesky.alpha.grandlyon.com', + }, + // Path to the built app in a particular language angularAppHost: { fr: '/fr', diff --git a/src/i18n/error-messages/error-messages.fr.ts b/src/i18n/error-messages/error-messages.fr.ts index e095bb68..66ad9bca 100644 --- a/src/i18n/error-messages/error-messages.fr.ts +++ b/src/i18n/error-messages/error-messages.fr.ts @@ -18,4 +18,7 @@ export const errors = { getDatasetChildren: 'Impossible de récupérer les enfants du jeu de données', getDatasetTitle: 'Impossible de récupérer le titre du jeu de données', }, + matomo: { + getViews: 'Impossible de récupérer le nombre de vues', + }, }; diff --git a/src/i18n/error-messages/error-messages.ts b/src/i18n/error-messages/error-messages.ts index 9850bf68..80a29ee0 100644 --- a/src/i18n/error-messages/error-messages.ts +++ b/src/i18n/error-messages/error-messages.ts @@ -18,4 +18,7 @@ export const errors = { getDatasetChildren: 'Failed to get the dataset children', getDatasetTitle: 'Failed to get the datatset title', }, + matomo: { + getViews: 'Cannot get the views count', + }, }; diff --git a/src/index.html b/src/index.html index a30b821c..670e89a9 100644 --- a/src/index.html +++ b/src/index.html @@ -20,19 +20,6 @@ </section> </app-root> - <script type="text/javascript"> - var _paq = _paq || []; - /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ - // _paq.push(['trackPageView']); // DELETE THIS LINE - _paq.push(['enableLinkTracking']); - (function() { - var u="//localhost:8181/"; - _paq.push(['setTrackerUrl', u+'piwik.php']); - _paq.push(['setSiteId', '1']); - 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> </body> </html> -- GitLab