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
33 changed files
with
357 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
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,2 @@ | ||
export * from './lib/pmp-web-shared-util-google-analytics.module'; | ||
export * from './lib/google-analytics.service'; |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { GoogleAnalyticsService } from './google-analytics.service'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
|
||
describe('GoogleAnalyticsService', () => { | ||
let service: GoogleAnalyticsService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [RouterTestingModule], | ||
providers: [GoogleAnalyticsService] | ||
}); | ||
service = TestBed.inject(GoogleAnalyticsService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
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() | ||
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); | ||
}); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...p-web/shared/util-google-analytics/src/lib/pmp-web-shared-util-google-analytics.module.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,9 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { GoogleAnalyticsService } from './google-analytics.service'; | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
providers: [GoogleAnalyticsService] | ||
}) | ||
export class PmpWebSharedUtilGoogleAnalyticsModule {} |
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
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-shell', | ||
preset: '../../../jest.config.js', | ||
coverageDirectory: '../../../coverage/libs/pmp-web/shell', | ||
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/pmp-web-shell.module'; |
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,18 @@ | ||
import { APP_INITIALIZER, NgModule } from '@angular/core'; | ||
import { | ||
GoogleAnalyticsService, | ||
PmpWebSharedUtilGoogleAnalyticsModule | ||
} from '@pimp-my-pr/pmp-web/shared/util-google-analytics'; | ||
|
||
@NgModule({ | ||
imports: [PmpWebSharedUtilGoogleAnalyticsModule], | ||
providers: [ | ||
{ | ||
provide: APP_INITIALIZER, | ||
useFactory: (googleService: GoogleAnalyticsService) => () => googleService.init(), | ||
deps: [GoogleAnalyticsService], | ||
multi: true | ||
} | ||
] | ||
}) | ||
export class PmpWebShellModule {} |
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"] | ||
} |
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"] | ||
} |
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"] | ||
} |
Oops, something went wrong.