forked from valueadd-poland/pimp-my-pr
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
22 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { Component } from '@angular/core'; | ||
// tslint:disable-next-line:nx-enforce-module-boundaries @ToDo how to handle such cases | ||
import { GoogleAnalyticsService } from '@pimp-my-pr/pmp-web/shared/util-google-analytics'; | ||
|
||
@Component({ | ||
selector: 'pmp-root', | ||
templateUrl: './app.component.html' | ||
}) | ||
export class AppComponent {} | ||
export class AppComponent { | ||
constructor(private googleAnalytics: GoogleAnalyticsService) { | ||
this.googleAnalytics.init(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag() { | ||
dataLayer.push(arguments); | ||
} | ||
gtag('js', new Date()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# pmp-web-shared-util-google-analytics | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test pmp-web-shared-util-google-analytics` to execute the unit tests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
name: 'pmp-web-shared-util-google-analytics', | ||
preset: '../../../../jest.config.js', | ||
coverageDirectory: '../../../../coverage/libs/pmp-web/shared/util-google-analytics', | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js', | ||
'jest-preset-angular/build/AngularSnapshotSerializer.js', | ||
'jest-preset-angular/build/HTMLCommentSerializer.js' | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/google-analytics.service'; |
16 changes: 16 additions & 0 deletions
16
libs/pmp-web/shared/util-google-analytics/src/lib/google-analytics.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { GoogleAnalyticsService } from './google-analytics.service'; | ||
|
||
describe('GoogleAnalyticsService', () => { | ||
let service: GoogleAnalyticsService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(GoogleAnalyticsService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
76 changes: 76 additions & 0 deletions
76
libs/pmp-web/shared/util-google-analytics/src/lib/google-analytics.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core'; | ||
import { DOCUMENT } from '@angular/common'; | ||
import { NavigationEnd, Router, RouterEvent } from '@angular/router'; | ||
import { environment } from '@pimp-my-pr/pmp-web/shared/config'; | ||
import { Observable } from 'rxjs'; | ||
import { filter, share, tap } from 'rxjs/operators'; | ||
|
||
declare let gtag: Function; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class GoogleAnalyticsService { | ||
private readonly renderer: Renderer2; | ||
private readonly googleId: string; | ||
private loaded = false; | ||
private loadingInProgress = false; | ||
|
||
constructor( | ||
private rendererFactory: RendererFactory2, | ||
@Inject(DOCUMENT) private document: Document, | ||
private router: Router | ||
) { | ||
this.renderer = rendererFactory.createRenderer(null, null); | ||
this.googleId = environment.googleAnalyticsId; | ||
} | ||
|
||
async init(): Promise<void> { | ||
if (this.loaded || this.loadingInProgress) return; | ||
try { | ||
await this.insertGoogleScript(this.googleId); | ||
await this.insertLocalScript(); | ||
this.loaded = true; | ||
} catch (error) { | ||
console.error(error); | ||
} finally { | ||
this.loadingInProgress = false; | ||
} | ||
} | ||
|
||
trackPageViews(): Observable<NavigationEnd> { | ||
return this.router.events.pipe( | ||
filter(() => this.loaded), | ||
filter((evt: RouterEvent) => evt instanceof NavigationEnd), | ||
tap((event: NavigationEnd) => { | ||
this.trackSinglePageView(event); | ||
}), | ||
share() | ||
); | ||
} | ||
|
||
private trackSinglePageView(event: NavigationEnd): void { | ||
gtag('config', this.googleId, { page_path: event.urlAfterRedirects }); | ||
} | ||
|
||
private async insertGoogleScript(googleId: string): Promise<void> { | ||
if (!googleId) throw new Error('googleId not set'); | ||
await this.insertScript(`https://www.googletagmanager.com/gtag/js?id=${googleId}`); | ||
} | ||
|
||
private async insertLocalScript(): Promise<void> { | ||
await this.insertScript('/assets/scripts/google-analytics-script.js'); | ||
} | ||
|
||
private insertScript(path: string): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
const script = this.renderer.createElement('script') as HTMLScriptElement; | ||
script.type = 'text/javascript'; | ||
script.onload = () => resolve(); | ||
script.onerror = () => reject(); | ||
script.src = path; | ||
script.text = path; | ||
this.renderer.appendChild(this.document.body, script); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'jest-preset-angular'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../../../tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node", "jest"] | ||
}, | ||
"include": ["**/*.ts"] | ||
} |
17 changes: 17 additions & 0 deletions
17
libs/pmp-web/shared/util-google-analytics/tsconfig.lib.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../../dist/out-tsc", | ||
"target": "es2015", | ||
"declaration": true, | ||
"inlineSources": true, | ||
"types": [], | ||
"lib": ["dom", "es2018"] | ||
}, | ||
"angularCompilerOptions": { | ||
"skipTemplateCodegen": true, | ||
"strictMetadataEmit": true, | ||
"enableResourceInlining": true | ||
}, | ||
"exclude": ["src/test-setup.ts", "**/*.spec.ts"] | ||
} |
10 changes: 10 additions & 0 deletions
10
libs/pmp-web/shared/util-google-analytics/tsconfig.spec.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"files": ["src/test-setup.ts"], | ||
"include": ["**/*.spec.ts", "**/*.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../../../tslint.json", | ||
"rules": { | ||
"directive-selector": [true, "attribute", "pimpMyPr", "camelCase"], | ||
"component-selector": [true, "element", "pimp-my-pr", "kebab-case"] | ||
}, | ||
"linterOptions": { | ||
"exclude": ["!**/*"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters