-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'angulartics:master' into ngx15
- Loading branch information
Showing
6 changed files
with
306 additions
and
0 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
Loading
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,11 @@ | ||
<img | ||
src="../../../assets/svg/posthog.svg" | ||
alt="Posthog logo" | ||
height="100px" | ||
width="200px" /> | ||
|
||
# Posthog | ||
|
||
**homepage**: [posthog.com](https://posthog.com/) | ||
**docs**: [posthog.com/docs/product-analytics](https://posthog.com/docs/product-analytics) | ||
**import**: `import { Angulartics2Posthog } from 'angulartics2';` |
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,169 @@ | ||
import { fakeAsync, inject, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { advance, createRoot, RootCmp, TestModule } from '../../test.mocks'; | ||
|
||
import { Angulartics2 } from '../../angulartics2-core'; | ||
import { Angulartics2Posthog } from './posthog'; | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; | ||
declare var window: any; | ||
|
||
describe('Angulartics2Posthog', () => { | ||
let fixture: ComponentFixture<any>; | ||
let posthog: any; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [TestModule], | ||
providers: [Angulartics2Posthog], | ||
}); | ||
|
||
window.posthog = posthog = { | ||
capture: jasmine.createSpy('capture'), | ||
identify: jasmine.createSpy('identify'), | ||
alias: jasmine.createSpy('alias'), | ||
}; | ||
|
||
const provider: Angulartics2Posthog = TestBed.inject(Angulartics2Posthog); | ||
provider.startTracking(); | ||
}); | ||
|
||
it('should track pages', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2Posthog], | ||
(angulartics2: Angulartics2, angulartics2Posthog: Angulartics2Posthog) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.pageTrack.next({ path: '/abc' }); | ||
advance(fixture); | ||
expect(posthog.capture).toHaveBeenCalledWith('Page Viewed', { | ||
page: '/abc', | ||
}); | ||
}, | ||
), | ||
)); | ||
|
||
it('should track events', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2Posthog], | ||
(angulartics2: Angulartics2, angulartics2Posthog: Angulartics2Posthog) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.eventTrack.next({ | ||
action: 'do', | ||
properties: { category: 'cat' }, | ||
}); | ||
advance(fixture); | ||
expect(posthog.capture).toHaveBeenCalledWith('do', { category: 'cat' }); | ||
}, | ||
), | ||
)); | ||
|
||
it('should set username', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2Posthog], | ||
(angulartics2: Angulartics2, angulartics2Posthog: Angulartics2Posthog) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.setUsername.next('testUser'); | ||
advance(fixture); | ||
expect(posthog.identify).toHaveBeenCalledWith('testUser'); | ||
}, | ||
), | ||
)); | ||
|
||
it('should set user properties', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2Posthog], | ||
(angulartics2: Angulartics2, angulartics2Posthog: Angulartics2Posthog) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.setUserProperties.next({ | ||
distinct_id: '1', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}); | ||
advance(fixture); | ||
expect(posthog.identify).toHaveBeenCalledWith('1', { | ||
distinct_id: '1', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}); | ||
}, | ||
), | ||
)); | ||
|
||
it('should set user properties once', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2Posthog], | ||
(angulartics2: Angulartics2, angulartics2Posthog: Angulartics2Posthog) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.setUserPropertiesOnce.next({ | ||
distinct_id: '1', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}); | ||
advance(fixture); | ||
expect(posthog.capture).toHaveBeenCalledWith('Set User Properties Once', { | ||
$set_once: { | ||
distinct_id: '1', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
} | ||
}); | ||
}, | ||
), | ||
)); | ||
|
||
it('should set super properties', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2Posthog], | ||
(angulartics2: Angulartics2, angulartics2Posthog: Angulartics2Posthog) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.setSuperProperties.next({ | ||
distinct_id: '1', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}); | ||
advance(fixture); | ||
expect(posthog.capture).toHaveBeenCalledWith('Set Super Properties', { | ||
$set: { | ||
distinct_id: '1', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
} | ||
}); | ||
}, | ||
), | ||
)); | ||
|
||
it('should set super properties once', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2Posthog], | ||
(angulartics2: Angulartics2, angulartics2Posthog: Angulartics2Posthog) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.setSuperPropertiesOnce.next({ | ||
distinct_id: '1', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}); | ||
advance(fixture); | ||
expect(posthog.capture).toHaveBeenCalledWith('Set Super Properties Once', { | ||
$set_once: { | ||
distinct_id: '1', | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
} | ||
}); | ||
}, | ||
), | ||
)); | ||
|
||
it('should set alias', fakeAsync( | ||
inject( | ||
[Angulartics2, Angulartics2Posthog], | ||
(angulartics2: Angulartics2, angulartics2Posthog: Angulartics2Posthog) => { | ||
fixture = createRoot(RootCmp); | ||
angulartics2.setAlias.next('testAlias'); | ||
advance(fixture); | ||
expect(posthog.alias).toHaveBeenCalledWith('testAlias'); | ||
}, | ||
), | ||
)); | ||
}); |
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,119 @@ | ||
import { inject, Injectable } from '@angular/core'; | ||
import { Angulartics2 } from '../../angulartics2-core'; | ||
|
||
declare let posthog: any; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class Angulartics2Posthog { | ||
private readonly angulartics2 = inject(Angulartics2); | ||
|
||
constructor() { | ||
this.angulartics2.setUsername.subscribe(x => this.setUsername(x)); | ||
this.angulartics2.setUserProperties.subscribe(x => this.setUserProperties(x)); | ||
this.angulartics2.setUserPropertiesOnce.subscribe(x => this.setUserPropertiesOnce(x)); | ||
this.angulartics2.setSuperProperties.subscribe(x => this.setSuperProperties(x)); | ||
this.angulartics2.setSuperPropertiesOnce.subscribe(x => this.setSuperPropertiesOnce(x)); | ||
this.angulartics2.setAlias.subscribe(x => this.setAlias(x)); | ||
} | ||
|
||
startTracking(): void { | ||
this.startPageTracking(); | ||
this.startEventTracking(); | ||
} | ||
|
||
startPageTracking(): void { | ||
this.angulartics2.pageTrack.pipe(this.angulartics2.filterDeveloperMode()).subscribe(x => { | ||
this.pageTrack(x.path); | ||
}); | ||
} | ||
|
||
startEventTracking(): void { | ||
this.angulartics2.eventTrack.pipe(this.angulartics2.filterDeveloperMode()).subscribe(x => { | ||
this.eventTrack(x.action, x.properties); | ||
}); | ||
} | ||
|
||
pageTrack(path: string | undefined): void { | ||
try { | ||
posthog.capture('Page Viewed', { page: path }); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
eventTrack(action: string, properties?: any): void { | ||
try { | ||
posthog.capture(action, properties); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
setUsername(userId: string | { userId: string | number }): void { | ||
try { | ||
posthog.identify(userId); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
setUserProperties(properties: any | undefined): void { | ||
try { | ||
posthog.identify(properties["distinct_id"], properties); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
setUserPropertiesOnce(properties: any | undefined): void { | ||
try { | ||
posthog.capture('Set User Properties Once', { | ||
$set_once: properties | ||
}); | ||
} | ||
catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
setSuperProperties(properties: any | undefined): void { | ||
try { | ||
posthog.capture('Set Super Properties', { $set: properties }); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
setSuperPropertiesOnce(properties: any | undefined): void { | ||
try { | ||
posthog.capture('Set Super Properties Once', { $set_once: properties }); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
setAlias(alias: string): void { | ||
try { | ||
posthog.alias(alias); | ||
} | ||
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