Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider): Added GoSquared provider (#341)
- **What kind of change does this PR introduce?** Addition of [GoSquared](https://gosquared.com) to the providers - **What is the current behavior? Link to open issue?** N/A - **What is the new behavior?** GoSquared provider is added with unit tests for the core functionality.
- Loading branch information
1 parent
a24968f
commit 104d419
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
<img | ||
src="../../../assets/svg/gosquared.svg" | ||
alt="GoSquared logo" | ||
height="100px" | ||
width="200px" /> | ||
|
||
# GoSquared | ||
__homepage__: [www.gosquared.com](https://www.gosquared.com/) | ||
__docs__: [gosquared.com/docs](https://gosquared.com/docs) | ||
__import__: `import { Angulartics2GoSquared } from 'angulartics2/gosquared';` | ||
|
||
## Setup |
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,114 @@ | ||
import { | ||
fakeAsync, | ||
inject, | ||
ComponentFixture, | ||
TestBed, | ||
} from '@angular/core/testing'; | ||
|
||
import { Angulartics2 } from 'angulartics2'; | ||
import { advance, createRoot, RootCmp, TestModule } from '../../test.mocks'; | ||
import { Angulartics2GoSquared } from './gosquared'; | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; | ||
declare var window: any; | ||
|
||
describe('Angulartics2GoSquared', () => { | ||
let fixture: ComponentFixture<any>; | ||
let _gs: any; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [TestModule], | ||
providers: [Angulartics2GoSquared] | ||
}); | ||
|
||
window._gs = _gs = jasmine.createSpy('_gs'); | ||
const provider: Angulartics2GoSquared = TestBed.get(Angulartics2GoSquared); | ||
provider.startTracking(); | ||
}); | ||
|
||
it('should track pages', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2GoSquared], | ||
( | ||
angulartics2: Angulartics2, | ||
angulartics2GoSquared: Angulartics2GoSquared | ||
) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.pageTrack.next({ path: '/abc' }); | ||
advance(fixture); | ||
expect(_gs).toHaveBeenCalledWith('track', '/abc'); | ||
} | ||
) | ||
)); | ||
|
||
it('should track events', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2GoSquared], | ||
( | ||
angulartics2: Angulartics2, | ||
angulartics2GoSquared: Angulartics2GoSquared | ||
) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.eventTrack.next({ | ||
action: 'do', | ||
properties: { category: 'cat' } | ||
}); | ||
advance(fixture); | ||
expect(_gs).toHaveBeenCalledWith('event', 'do', { | ||
category: 'cat' | ||
}); | ||
} | ||
) | ||
)); | ||
|
||
it('should set user properties', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2GoSquared], | ||
( | ||
angulartics2: Angulartics2, | ||
angulartics2GoSquared: Angulartics2GoSquared | ||
) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.setUserProperties.next({ | ||
id: '1', | ||
email: '1', | ||
first_name: 'John', | ||
last_name: 'Doe' | ||
}); | ||
advance(fixture); | ||
expect(_gs).toHaveBeenCalledWith('identify', { | ||
id: '1', | ||
email: '1', | ||
first_name: 'John', | ||
last_name: 'Doe' | ||
}); | ||
} | ||
) | ||
)); | ||
|
||
it('should set user properties once', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2GoSquared], | ||
( | ||
angulartics2: Angulartics2, | ||
angulartics2GoSquared: Angulartics2GoSquared | ||
) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.setUserPropertiesOnce.next({ | ||
id: '1', | ||
email: '1', | ||
first_name: 'John', | ||
last_name: 'Doe' | ||
}); | ||
advance(fixture); | ||
expect(_gs).toHaveBeenCalledWith('identify', { | ||
id: '1', | ||
email: '1', | ||
first_name: 'John', | ||
last_name: 'Doe' | ||
}); | ||
} | ||
) | ||
)); | ||
}); |
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,56 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
import { Angulartics2 } from 'angulartics2'; | ||
|
||
declare var _gs: any; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class Angulartics2GoSquared { | ||
constructor(private angulartics2: Angulartics2) { | ||
this.angulartics2.setUserProperties.subscribe(x => | ||
this.setUserProperties(x), | ||
); | ||
this.angulartics2.setUserPropertiesOnce.subscribe(x => | ||
this.setUserProperties(x), | ||
); | ||
} | ||
|
||
startTracking(): void { | ||
this.angulartics2.pageTrack | ||
.pipe(this.angulartics2.filterDeveloperMode()) | ||
.subscribe(x => this.pageTrack(x.path)); | ||
this.angulartics2.eventTrack | ||
.pipe(this.angulartics2.filterDeveloperMode()) | ||
.subscribe(x => this.eventTrack(x.action, x.properties)); | ||
} | ||
|
||
pageTrack(path: string) { | ||
try { | ||
_gs('track', path); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
eventTrack(action: string, properties: any) { | ||
try { | ||
_gs('event', action, properties); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
setUserProperties(properties: any) { | ||
try { | ||
_gs('identify', properties); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
{ | ||
"$schema": "../../../../node_modules/ng-packagr/package.schema.json", | ||
"name": "angulartics2/gosquared", | ||
"description": "The gosquared module", | ||
"homepage": "https://angulartics.github.io/angulartics2/", | ||
"author": "Ben White <ben@benjackwhite.co.uk> (http://github.com/benjackwhite)", | ||
"repository": "angulartics/angulartics2", | ||
"license": "MIT", | ||
"ngPackage": { | ||
"lib": { | ||
"entryFile": "gosquared.ts", | ||
"umdModuleIds": { | ||
"angulartics2": "angulartics2" | ||
} | ||
}, | ||
"dest": "../../../../dist/packages-dist/gosquared" | ||
} | ||
} |