This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
8,822 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Editor configuration, see https://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
max_line_length = off | ||
trim_trailing_whitespace = false |
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,44 @@ | ||
# See http://help.github.com/ignore-files/ for more about ignoring files. | ||
|
||
# compiled output | ||
/dist | ||
/tmp | ||
/out-tsc | ||
|
||
# dependencies | ||
/node_modules | ||
|
||
# profiling files | ||
chrome-profiler-events.json | ||
speed-measure-plugin.json | ||
|
||
# IDEs and editors | ||
/.idea | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
.history/* | ||
|
||
# misc | ||
/.sass-cache | ||
/connect.lock | ||
/coverage | ||
/libpeerconnection.log | ||
npm-debug.log | ||
yarn-error.log | ||
testem.log | ||
/typings | ||
|
||
# System Files | ||
.DS_Store | ||
Thumbs.db |
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,2 +1,27 @@ | ||
# gtm-cookie-consent | ||
An Angular library to active Google Tag Manager only after the user agreed to use cookies | ||
# GtmCookieConsent | ||
|
||
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.0. | ||
|
||
## Development server | ||
|
||
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. | ||
|
||
## Code scaffolding | ||
|
||
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. | ||
|
||
## Build | ||
|
||
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. | ||
|
||
## Running unit tests | ||
|
||
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). | ||
|
||
## Running end-to-end tests | ||
|
||
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). | ||
|
||
## Further help | ||
|
||
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). |
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,43 @@ | ||
{ | ||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json", | ||
"version": 1, | ||
"newProjectRoot": "projects", | ||
"projects": { | ||
"ngx-gtm-cookie-consent": { | ||
"root": "projects/ngx-gtm-cookie-consent", | ||
"sourceRoot": "projects/ngx-gtm-cookie-consent/src", | ||
"projectType": "library", | ||
"prefix": "lib", | ||
"architect": { | ||
"build": { | ||
"builder": "@angular-devkit/build-ng-packagr:build", | ||
"options": { | ||
"tsConfig": "projects/ngx-gtm-cookie-consent/tsconfig.lib.json", | ||
"project": "projects/ngx-gtm-cookie-consent/ng-package.json" | ||
} | ||
}, | ||
"test": { | ||
"builder": "@angular-devkit/build-angular:karma", | ||
"options": { | ||
"main": "projects/ngx-gtm-cookie-consent/src/test.ts", | ||
"tsConfig": "projects/ngx-gtm-cookie-consent/tsconfig.spec.json", | ||
"karmaConfig": "projects/ngx-gtm-cookie-consent/karma.conf.js" | ||
} | ||
}, | ||
"lint": { | ||
"builder": "@angular-devkit/build-angular:tslint", | ||
"options": { | ||
"tsConfig": [ | ||
"projects/ngx-gtm-cookie-consent/tsconfig.lib.json", | ||
"projects/ngx-gtm-cookie-consent/tsconfig.spec.json" | ||
], | ||
"exclude": [ | ||
"**/node_modules/**" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"defaultProject": "ngx-gtm-cookie-consent" | ||
} |
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,53 @@ | ||
{ | ||
"name": "ngx-gtm-cookie-consent", | ||
"version": "0.0.1", | ||
"scripts": { | ||
"build": "ng build ngx-gtm-cookie-consent" | ||
}, | ||
"private": false, | ||
"author": { | ||
"name": "Exportarts", | ||
"email": "[email protected]", | ||
"url": "https://www.exportarts.io" | ||
}, | ||
"dependencies": { | ||
"@angular/animations": "~7.2.0", | ||
"@angular/common": "~7.2.0", | ||
"@angular/compiler": "~7.2.0", | ||
"@angular/core": "~7.2.0", | ||
"@angular/forms": "~7.2.0", | ||
"@angular/platform-browser": "~7.2.0", | ||
"@angular/platform-browser-dynamic": "~7.2.0", | ||
"@angular/router": "~7.2.0", | ||
"core-js": "^2.5.4", | ||
"ngx-cookieconsent": "^2.2.3", | ||
"rxjs": "~6.3.3", | ||
"tslib": "^1.9.0", | ||
"zone.js": "~0.8.26" | ||
}, | ||
"devDependencies": { | ||
"@angular-devkit/build-angular": "~0.13.0", | ||
"@angular-devkit/build-ng-packagr": "~0.13.0", | ||
"@angular/cli": "~7.3.0", | ||
"@angular/compiler-cli": "~7.2.0", | ||
"@angular/language-service": "~7.2.0", | ||
"@types/jasmine": "~2.8.8", | ||
"@types/jasminewd2": "~2.0.3", | ||
"@types/node": "~8.9.4", | ||
"codelyzer": "~4.5.0", | ||
"jasmine-core": "~2.99.1", | ||
"jasmine-spec-reporter": "~4.2.1", | ||
"karma": "~3.1.1", | ||
"karma-chrome-launcher": "~2.2.0", | ||
"karma-coverage-istanbul-reporter": "~2.0.1", | ||
"karma-jasmine": "~1.1.2", | ||
"karma-jasmine-html-reporter": "^0.2.2", | ||
"ng-packagr": "^4.2.0", | ||
"protractor": "~5.4.0", | ||
"ts-node": "~7.0.0", | ||
"tsickle": ">=0.34.0", | ||
"tslib": "^1.9.0", | ||
"tslint": "~5.11.0", | ||
"typescript": "~3.2.2" | ||
} | ||
} |
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,24 @@ | ||
# GtmCookieConsent | ||
|
||
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.2.0. | ||
|
||
## Code scaffolding | ||
|
||
Run `ng generate component component-name --project ngx-gtm-cookie-consent` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project ngx-gtm-cookie-consent`. | ||
> Note: Don't forget to add `--project ngx-gtm-cookie-consent` or else it will be added to the default project in your `angular.json` file. | ||
## Build | ||
|
||
Run `ng build ngx-gtm-cookie-consent` to build the project. The build artifacts will be stored in the `dist/` directory. | ||
|
||
## Publishing | ||
|
||
After building your library with `ng build ngx-gtm-cookie-consent`, go to the dist folder `cd dist/ngx-gtm-cookie-consent` and run `npm publish`. | ||
|
||
## Running unit tests | ||
|
||
Run `ng test ngx-gtm-cookie-consent` to execute the unit tests via [Karma](https://karma-runner.github.io). | ||
|
||
## Further help | ||
|
||
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). |
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,31 @@ | ||
// Karma configuration file, see link for more information | ||
// https://karma-runner.github.io/1.0/config/configuration-file.html | ||
|
||
module.exports = function (config) { | ||
config.set({ | ||
basePath: '', | ||
frameworks: ['jasmine', '@angular-devkit/build-angular'], | ||
plugins: [ | ||
require('karma-jasmine'), | ||
require('karma-chrome-launcher'), | ||
require('karma-jasmine-html-reporter'), | ||
require('karma-coverage-istanbul-reporter'), | ||
require('@angular-devkit/build-angular/plugins/karma') | ||
], | ||
client: { | ||
clearContext: false // leave Jasmine Spec Runner output visible in browser | ||
}, | ||
coverageIstanbulReporter: { | ||
dir: require('path').join(__dirname, '../../coverage/ngx-gtm-cookie-consent'), | ||
reports: ['html', 'lcovonly'], | ||
fixWebpackSourcePaths: true | ||
}, | ||
reporters: ['progress', 'kjhtml'], | ||
port: 9876, | ||
colors: true, | ||
logLevel: config.LOG_INFO, | ||
autoWatch: true, | ||
browsers: ['Chrome'], | ||
singleRun: false | ||
}); | ||
}; |
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 @@ | ||
{ | ||
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json", | ||
"dest": "../../dist/ngx-gtm-cookie-consent", | ||
"lib": { | ||
"entryFile": "src/public_api.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 @@ | ||
{ | ||
"name": "ngx-gtm-cookie-consent", | ||
"version": "0.0.1", | ||
"peerDependencies": { | ||
"@angular/common": "^7.2.0", | ||
"@angular/core": "^7.2.0", | ||
"@angular/router": "^7.2.0", | ||
"ngx-cookieconsent": "^2.2.3" | ||
} | ||
} |
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,35 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core'; | ||
import { CookieConsentServiceConfig, COOKIE_CONSENT_SERVICE_CONFIG } from './services/cookie-consent.config'; | ||
import { CookieConsentService } from './services/cookie-consent.service'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule | ||
] | ||
}) | ||
export class CookieModule { | ||
|
||
constructor( | ||
@Optional() @SkipSelf() | ||
parentModule: CookieModule | ||
) { | ||
if (parentModule) { | ||
throw new Error('CookieModule is already loaded. Import it in the AppModule only'); | ||
} | ||
} | ||
|
||
static forRoot(config: CookieConsentServiceConfig): ModuleWithProviders { | ||
return { | ||
ngModule: CookieModule, | ||
providers: [ | ||
{ | ||
provide: COOKIE_CONSENT_SERVICE_CONFIG, | ||
useValue: config | ||
}, | ||
CookieConsentService | ||
] | ||
}; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
projects/ngx-gtm-cookie-consent/src/lib/js/cookie-consent.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,64 @@ | ||
function getCookie(name) { | ||
return !name ? '' : decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(name).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; | ||
} | ||
|
||
function setupGTM() { | ||
(function (w, d, s, l, i) { | ||
// @ts-ignore (TODO: Fix TS errors) | ||
w[l] = w[l] || []; w[l].push({ | ||
'gtm.start': new Date().getTime(), event: 'gtm.js' | ||
// @ts-ignore (TODO: Fix TS errors) | ||
}); var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src = | ||
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f); | ||
// @ts-ignore (TODO: Fix TS errors) | ||
})(window, document, 'script', window.exportarts.tracking.dataLayerName, window.exportarts.tracking.gtmContainerId); | ||
} | ||
|
||
function main() { | ||
// Only execute the script if run in a browser context (Angular Universal support) | ||
if (typeof window !== 'undefined') { | ||
// @ts-ignore (TODO: Fix TS errors) | ||
window.exportarts = window.exportarts || {}; | ||
// @ts-ignore (TODO: Fix TS errors) | ||
window.exportarts.tracking = window.exportarts.tracking || {}; | ||
|
||
// @ts-ignore (TODO: Fix TS errors) | ||
window.exportarts.cookieConsent = { | ||
eventName: 'expo-cookieconsent-change', | ||
listener: () => { | ||
// @ts-ignore (TODO: Fix TS errors) | ||
if (!window.exportarts.tracking.gtmContainerId) { | ||
console.warn('GTM tracking is disabled. Set window.exportarts.tracking.gtmContainerId to activate it.'); | ||
return; | ||
} | ||
// @ts-ignore (TODO: Fix TS errors) | ||
if (!window.exportarts.tracking.enabled) { | ||
console.warn('GTM tracking is disabled. Set window.exportarts.tracking.enabled to activate it.'); | ||
return; | ||
} | ||
|
||
// @ts-ignore (TODO: Fix TS errors) | ||
const cookie = getCookie(window.exportarts.cookieConsent.cookieName); | ||
if (cookie === 'allow') { | ||
// Make sure dataLayer is present | ||
// @ts-ignore (TODO: Fix TS errors) | ||
window[window.exportarts.tracking.dataLayerName] = window[window.exportarts.tracking.dataLayerName] || []; | ||
|
||
setupGTM(); | ||
|
||
// Listener is not needed anymore | ||
// @ts-ignore (TODO: Fix TS errors) | ||
window.removeEventListener(window.exportarts.cookieConsent.eventName, window.exportarts.cookieConsent.listener); | ||
} | ||
} | ||
} | ||
|
||
// Listen for changes in the user's cookieconsent status | ||
// @ts-ignore (TODO: Fix TS errors) | ||
window.addEventListener(window.exportarts.cookieConsent.eventName, window.exportarts.cookieConsent.listener); | ||
} | ||
} | ||
|
||
export const _null = null; | ||
|
||
main(); |
Oops, something went wrong.