Skip to content

Commit

Permalink
Merge branch 'angulartics:master' into ngx15
Browse files Browse the repository at this point in the history
  • Loading branch information
rnsoon authored Jun 10, 2024
2 parents a455576 + be4ea61 commit 29f3853
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/app/providers/providers.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export class ProvidersComponent {
display: 'Matomo',
type: 'Analytics',
},
{
name: 'posthog',
display: 'Posthog',
type: 'Analytics',
},
{
name: 'segment',
display: 'Segment',
Expand Down
1 change: 1 addition & 0 deletions src/assets/svg/posthog.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/lib/providers/posthog/README.md
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';`
169 changes: 169 additions & 0 deletions src/lib/providers/posthog/posthog.spec.ts
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');
},
),
));
});
119 changes: 119 additions & 0 deletions src/lib/providers/posthog/posthog.ts
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;
}
}
}
}
1 change: 1 addition & 0 deletions src/lib/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './providers/hubspot/hubspot';
export * from './providers/kissmetrics/kissmetrics';
export * from './providers/launch/launch';
export * from './providers/mixpanel/mixpanel';
export * from './providers/posthog/posthog';
export * from './providers/pyze/pyze';
export * from './providers/matomo/matomo';
export * from './providers/segment/segment';
Expand Down

0 comments on commit 29f3853

Please sign in to comment.