Skip to content

Commit

Permalink
feat(analytics): add matomo to analyse website
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Mar 22, 2018
1 parent 572c84d commit 575b62c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lib/analytics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './module';
export * from './shared';
18 changes: 18 additions & 0 deletions src/lib/analytics/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NgModule, ModuleWithProviders } from '@angular/core';

import { AnalyticsService } from './shared/analytics.service';

@NgModule({
imports: [],
declarations: [],
exports: []
})

export class IgoAnalyticsModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: IgoAnalyticsModule,
providers: [AnalyticsService]
};
}
}
7 changes: 7 additions & 0 deletions src/lib/analytics/shared/analytics.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type AnalyticsProvider = 'matomo';

export interface AnalyticsOptions {
provider?: AnalyticsProvider;
url?: string;
id?: string;
}
18 changes: 18 additions & 0 deletions src/lib/analytics/shared/analytics.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// import { HttpClient } from '@angular/common/http';
//
// import { Angulartics2Module } from 'angulartics2';
// import { Angulartics2Piwik } from 'angulartics2/piwik';
//
// import { ConfigService } from '../../core/config';
//
// export function matomoAnalyticsFactory(http: HttpClient, config?: ConfigService) {
// return new LanguageLoader(http, undefined, undefined, config);
// }
//
// export function provideMatomoAnalytics() {
// return {
// provide: MatomoAnalytics,
// useFactory: (defaultLanguageLoader),
// deps: [Angulartics2Piwik]
// };
// }
40 changes: 40 additions & 0 deletions src/lib/analytics/shared/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Injectable } from '@angular/core';

import { ConfigService } from '../../core/config';

import { AnalyticsOptions } from './analytics.interface';

@Injectable()
export class AnalyticsService {

private options: AnalyticsOptions;

constructor(private config: ConfigService) {
this.options = this.config.getConfig('analytics') || {};

if (this.options.provider === 'matomo') {
this.initMatomo();
}
}

private initMatomo() {
if (!this.options.url || !this.options.id) {
return;
}

window['_paq'] = window['_paq'] || [];
window['_paq'].push(['trackPageView']);
window['_paq'].push(['enableLinkTracking']);
(() => {
window['_paq'].push(['setTrackerUrl', this.options.url + 'piwik.php']);
window['_paq'].push(['setSiteId', this.options.id]);
const g = document.createElement('script');
const s = document.getElementsByTagName('script')[0];
g.type = 'text/javascript';
g.async = true;
g.defer = true;
g.src = this.options.url + 'piwik.js';
s.parentNode.insertBefore(g, s);
})();
}
}
2 changes: 2 additions & 0 deletions src/lib/analytics/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './analytics.service';
export * from './analytics.interface';
3 changes: 2 additions & 1 deletion src/lib/environment.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { SearchSourcesOptions, LanguageOptions, AuthOptions,
import { SearchSourcesOptions, LanguageOptions, AuthOptions, AnalyticsOptions,
ContextServiceOptions, CatalogServiceOptions, ImportExportServiceOptions
} from './';

export interface IgoEnvironment {
analytics?: AnalyticsOptions;
searchSources?: SearchSourcesOptions;
language?: LanguageOptions;
auth?: AuthOptions;
Expand Down
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './module';

export * from './analytics';
export * from './auth';
export * from './core';
export * from './context';
Expand Down
3 changes: 3 additions & 0 deletions src/lib/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CustomMaterialModule } from './customMaterialModule';
import 'hammerjs/hammer';
import 'rxjs/add/observable/empty';

import { IgoAnalyticsModule } from './analytics';
import { IgoAuthModule } from './auth';
import { IgoCoreModule } from './core';
import { IgoContextModule } from './context';
Expand All @@ -28,6 +29,7 @@ import { IgoShareMapModule } from './share-map';
import { IgoToolModule } from './tool';

const IGO_MODULES = [
IgoAnalyticsModule,
IgoAuthModule,
IgoCoreModule,
IgoContextModule,
Expand All @@ -54,6 +56,7 @@ const IGO_MODULES = [
imports: [
CustomMaterialModule,

IgoAnalyticsModule.forRoot(),
IgoAuthModule.forRoot(),
IgoCoreModule.forRoot(),
IgoContextModule.forRoot(),
Expand Down

0 comments on commit 575b62c

Please sign in to comment.