Skip to content

Commit

Permalink
feat: convert TrackedEvent to interface
Browse files Browse the repository at this point in the history
BREAKING CHANGE: TrackedEvent is now an interface instead of an abstract base class so applications aren't tied to a particular implementation for concrete events.
  • Loading branch information
yangchristian committed Sep 14, 2017
1 parent 8e11f3c commit 4351eb6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Coverage Status](https://coveralls.io/repos/github/CrossLead/crosslytics/badge.svg?branch=master)](https://coveralls.io/github/CrossLead/crosslytics?branch=master)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

Tired of writing repetitive code to send the same analytics data to all your different tracking services? Or formatting the same data slightly differently for each service, like using ["eventAction"](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#eventAction) for Google Analytics but ["event_name"](https://developers.intercom.com/reference#event-model) for Intercom?
Tired of writing repetitive code to send the same analytics data to all your different tracking services? Or formatting the same data slightly differently for each service, like using [`eventAction`](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#eventAction) for Google Analytics but [`event_name`](https://developers.intercom.com/reference#event-model) for Intercom?

Use Crosslytics for a unified event definition and analytics reporting API: Define events once and report them to all your analytics services with a single call. We've also abstracted the services themselves into a pluggable [Tracker](#trackers) architecture so you can use only the services you need and quickly add support for new ones.

Expand All @@ -21,7 +21,7 @@ type DashboardPanelEventArgs = {
'Panel Name'?: string
};

export class DashboardPanelCreated extends TrackedEvent<DashboardPanelEventArgs> {
export class DashboardPanelCreated implements TrackedEvent<DashboardPanelEventArgs> {
readonly name = 'DashboardPanel Created';
readonly category = 'Dashboard';
readonly argPriority: (keyof DashboardPanelEventArgs)[] = [
Expand Down
6 changes: 3 additions & 3 deletions src/core/crosslytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ interface TestEventArgs {
'Color': string;
}

class TestEvent extends TrackedEvent<TestEventArgs> {
class TestEvent implements TrackedEvent<TestEventArgs> {
public name = 'Test Event';
public category = 'Test Category';
public organizationId = 'abc123';
public argPriority = new Array<keyof TestEventArgs>();
public argPriority: Array<keyof TestEventArgs> = ['Color'];
constructor(public args: TestEventArgs) {}
}

// tslint:disable-next-line:max-classes-per-file
Expand Down
10 changes: 4 additions & 6 deletions src/core/crosslytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export class Crosslytics {
* Helper method equivalent to calling .track() on all registered trackers
*/
public async track<T>(event: TrackedEvent<T>) {
const promises = Array
.from(this.trackers.values())
.map(t => t.track(event));
const promises = Array.from(this.trackers.values()).map(t =>
t.track(event)
);
return Promise.all(promises);
}

Expand All @@ -25,9 +25,7 @@ export class Crosslytics {
public async page(page: Page | string) {
const p: Page = typeof page === 'string' ? { url: page } : page;

const promises = Array
.from(this.trackers.values())
.map(t => t.page(p));
const promises = Array.from(this.trackers.values()).map(t => t.page(p));

return Promise.all(promises);
}
Expand Down
15 changes: 8 additions & 7 deletions src/core/trackedEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* 'Panel Type'?: number,
* 'Panel Name'?: string
* };
* class DashboardPanelCreated extends TrackedEvent<DashboardPanelEventArgs> {
* class DashboardPanelCreated implements TrackedEvent<DashboardPanelEventArgs> {
* readonly name = 'DashboardPanel Created';
* readonly category = 'Dashboard';
* readonly argPriority: (keyof DashboardPanelEventArgs)[] = [
Expand All @@ -17,16 +17,19 @@
* 'Panel Name',
* 'Panel Color'
* ];
* constructor(public args: DashboardPanelEventArgs) {};
* }
* @see {@link https://segment.com/docs/spec/track/#event}
*/
export abstract class TrackedEvent<T = {}> {
export interface TrackedEvent<T = {}> {
/**
* We suggest human readable names consisting of noun + past tense verb.
* @see {@link https://segment.com/academy/collecting-data/naming-conventions-for-clean-data/}
*/
public abstract readonly name: string;
public abstract readonly category: string;
readonly name: string;
readonly category: string;

args: T;

/**
* Many trackers only support a limited number of arguments. For example,
Expand All @@ -36,7 +39,5 @@ export abstract class TrackedEvent<T = {}> {
* the Google Analytics case, the tracker will submit the first string match
* as the Label and the first integer match as the Value.
*/
public abstract readonly argPriority: Array<keyof T>;

constructor(public args: T) {}
readonly argPriority: Array<keyof T>;
}

0 comments on commit 4351eb6

Please sign in to comment.