Skip to content

Commit

Permalink
feat: flag to remove hashes (#) from url path (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalina authored and scttcper committed Mar 19, 2018
1 parent 879f57d commit 877c4b4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Vendor-agnostic Analytics for Angular Applications. [angulartics.github.io/angul
- [Exclude routes from automatic pageview tracking](#exclude-routes-from-automatic-pageview-tracking)
- [Remove IDs from url paths](#remove-ids-from-url-paths)
- [Remove Query Params from url paths](#remove-query-params-from-url-paths)
- [Remove Hash from url paths](#remove-hash-from-url-paths)
- [Using Without A Router](#using-without-a-router)
- [Using With UI-Router](#using-with-ui-router)
- [SystemJS](#systemjs)
Expand Down Expand Up @@ -202,6 +203,17 @@ Angulartics2Module.forRoot([providers], {
}),
````

#### Remove Hash from url paths

`/callback#authcode=123&idToken=456` becomes `/callback`
````ts
Angulartics2Module.forRoot([providers], {
pageTracking: {
clearHash: true,
}
}),
````

### Using Without A Router

__Warning:__ this support is still experiemental
Expand Down
3 changes: 3 additions & 0 deletions src/lib/core/angulartics2-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface PageTrackingSettings {
excludedRoutes: (string | RegExp)[];
/** drop ids from url `/sections/123/pages/456` -> `/sections/pages` */
clearIds: boolean;
/** drop contents of url after hash marker `/callback#authcode=1234` -> `/callback` */
clearHash: boolean;
/** drop query params from url `/sections/123/pages?param=456&param2=789` -> `/sections/123/pages` */
clearQueryParams: boolean;
/** used with clearIds, define the matcher to clear url parts */
Expand All @@ -41,6 +43,7 @@ export class DefaultConfig implements Angulartics2Settings {
basePath: '',
excludedRoutes: [],
clearIds: false,
clearHash: false,
clearQueryParams: false,
idsRegExp: /^\d+$|^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/,
};
Expand Down
12 changes: 12 additions & 0 deletions src/lib/core/angulartics2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,18 @@ describe('angulartics2', () => {
})),
);

it('should remove hash if clearHash is set',
fakeAsync(inject([Router, Location, Angulartics2],
(router: Router, location: Location, angulartics2: Angulartics2) => {
fixture = createRootWithRouter(router, RootCmp);
angulartics2.pageTrack.subscribe((x: any) => EventSpy(x));
angulartics2.settings.pageTracking.clearHash = true;
(<SpyLocation>location).simulateUrlPop('/0sections0/a01/pages/page/2/summary#authCode=123');
advance(fixture);
expect(EventSpy).toHaveBeenCalledWith({ path: '/0sections0/a01/pages/page/2/summary' });
})),
);

it('should remove ids and query params if clearQueryParams and clearIds are set',
fakeAsync(inject([Router, Location, Angulartics2],
(router: Router, location: Location, angulartics2: Angulartics2) => {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/core/angulartics2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ export class Angulartics2 {
* @param url current page path
*/
protected clearUrl(url: string): string {
if (this.settings.pageTracking.clearIds || this.settings.pageTracking.clearQueryParams) {
if (this.settings.pageTracking.clearIds || this.settings.pageTracking.clearQueryParams ||
this.settings.pageTracking.clearHash) {
return url
.split('/')
.map(part => this.settings.pageTracking.clearQueryParams ? part.split('?')[0] : part)
.map(part => this.settings.pageTracking.clearHash ? part.split('#')[0] : part)
.filter(part => !this.settings.pageTracking.clearIds || !part.match(this.settings.pageTracking.idsRegExp))
.join('/');
}
Expand Down

0 comments on commit 877c4b4

Please sign in to comment.