Skip to content

Commit

Permalink
feat(provider): GA anonymizeIP (angulartics#251)
Browse files Browse the repository at this point in the history
feat(provider): GA anonymizeIP
  • Loading branch information
marvec authored and scttcper committed Mar 22, 2018
1 parent 877c4b4 commit 85264bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/core/angulartics2-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/providers/ga/angulartics2-ga.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}),
),
);

});
7 changes: 7 additions & 0 deletions src/lib/providers/ga/angulartics2-ga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class GoogleAnalyticsDefaults implements GoogleAnalyticsSettings {
additionalAccountNames = [];
userId = null;
transport = '';
anonymizeIp = false;
}

@Injectable()
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 85264bf

Please sign in to comment.