Skip to content

Commit

Permalink
feat(provider): Added GoSquared provider (#341)
Browse files Browse the repository at this point in the history
- **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
benjackwhite authored and scttcper committed Apr 16, 2019
1 parent a24968f commit 104d419
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/assets/svg/gosquared.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/lib/providers/gosquared/README.md
@@ -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
114 changes: 114 additions & 0 deletions src/lib/providers/gosquared/gosquared.spec.ts
@@ -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'
});
}
)
));
});
56 changes: 56 additions & 0 deletions src/lib/providers/gosquared/gosquared.ts
@@ -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;
}
}
}
}
18 changes: 18 additions & 0 deletions src/lib/providers/gosquared/package.json
@@ -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"
}
}

0 comments on commit 104d419

Please sign in to comment.