From 85264bf4f021592a16667fe07078ba2c119fc7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ve=C4=8De=C5=99a?= Date: Thu, 22 Mar 2018 21:48:22 +0100 Subject: [PATCH] feat(provider): GA anonymizeIP (#251) feat(provider): GA anonymizeIP --- src/lib/core/angulartics2-config.ts | 1 + src/lib/providers/ga/angulartics2-ga.spec.ts | 12 ++++++++++++ src/lib/providers/ga/angulartics2-ga.ts | 7 +++++++ 3 files changed, 20 insertions(+) diff --git a/src/lib/core/angulartics2-config.ts b/src/lib/core/angulartics2-config.ts index c7cccc43..4e833e51 100644 --- a/src/lib/core/angulartics2-config.ts +++ b/src/lib/core/angulartics2-config.ts @@ -4,6 +4,7 @@ export interface GoogleAnalyticsSettings { userId: any; /** see https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport */ transport: string; + anonymizeIp: boolean; } export interface AppInsightsSettings { diff --git a/src/lib/providers/ga/angulartics2-ga.spec.ts b/src/lib/providers/ga/angulartics2-ga.spec.ts index 5debb7ea..83d042f4 100644 --- a/src/lib/providers/ga/angulartics2-ga.spec.ts +++ b/src/lib/providers/ga/angulartics2-ga.spec.ts @@ -183,4 +183,16 @@ describe('Angulartics2GoogleAnalytics', () => { )), ); + it('should anonymize IP', + fakeAsync(inject([Angulartics2, Angulartics2GoogleAnalytics], + (angulartics2: Angulartics2, angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) => { + fixture = createRoot(RootCmp); + angulartics2.settings.ga.anonymizeIp = true; + angulartics2.pageTrack.next({ path: '/abc' }); + advance(fixture); + expect(ga).toHaveBeenCalledWith('set', 'anonymizeIp', true); + }), + ), + ); + }); diff --git a/src/lib/providers/ga/angulartics2-ga.ts b/src/lib/providers/ga/angulartics2-ga.ts index fdb08821..5895737a 100644 --- a/src/lib/providers/ga/angulartics2-ga.ts +++ b/src/lib/providers/ga/angulartics2-ga.ts @@ -15,6 +15,7 @@ export class GoogleAnalyticsDefaults implements GoogleAnalyticsSettings { additionalAccountNames = []; userId = null; transport = ''; + anonymizeIp = false; } @Injectable() @@ -58,6 +59,12 @@ export class Angulartics2GoogleAnalytics { ga(accountName + '.set', '&uid', this.angulartics2.settings.ga.userId); } } + if (this.angulartics2.settings.ga.anonymizeIp) { + ga('set', 'anonymizeIp', true); + for (const accountName of this.angulartics2.settings.ga.additionalAccountNames) { + ga(accountName + '.set', 'anonymizeIp', true); + } + } ga('send', 'pageview', path); for (const accountName of this.angulartics2.settings.ga.additionalAccountNames) { ga(accountName + '.send', 'pageview', path);