An Angular module wrapper for Segment's official analytics-next
JS library.
It is inspired by @opendecide/ngx-segment-analytic
To install this library, run:
$ yarn add ngx-analytics-next
Add the AnalyticsNextModule
to your Angular AppModule
:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// Import the Analytics Next module
import { AnalyticsNextModule } from '@cartona/ngx-analytics-next';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
// Analytics Next Importation
AnalyticsNextModule.forRoot({ writeKey: 'YOUR_WRITE_APIKEY' })
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
You can use the AnalyticsNextService
in any constructor as a injected service :
import { Component, OnInit } from '@angular/core';
import { AnalyticsNextService } from '@cartona/ngx-analytics-next';
@Component({
selector: 'hero',
templateUrl: './hero.component.html',
styleUrls: ['./hero.component.css']
})
export class HeroComponent implements OnInit {
constructor(private analyticsService: AnalyticsNextService) {
}
public ngOnInit() {
this.analyticsService.track('load an hero')
.then(() => console.log("Event sended"));
}
}
This API is compatible with analytics.js
but returns Promises
instead of taking callbacks
in parameters.
load(apiKey: string, options: any);
get plugins: {[pluginName :string]: SegmentPlugin};
identify(userId?: string, traits?: any, options?: any): Promise<SegmentService>;
track(event: string, properties?: any, options?: any): Promise<SegmentService>;
page(category?: string, name?: string, properties?: any, options?: any): Promise<SegmentService>;
group(groupId: string, traits?: any): Promise<SegmentService>;
alias(userId: string, previousId?: string, options?: any): Promise<SegmentService>;
ready(): Promise<SegmentService>;
user(): any;
id(): any;
traits(): any;
reset(): void;
debug(enabled?: boolean): void;
on(method: string, callback: (event?: string, properties?: any, options?: any) => any): void;
trackLink(elements: HTMLElement | HTMLElement[], event: string | Function, properties?: Object | Function): void;
trackForm(forms: HTMLElement | HTMLElement[], event: string | Function, properties?: Object | Function): void;
timeout(timeout: number): void;
addSourceMiddleware(middleware: ({integrations, payload, next}) => void): void;
To lint all *.ts
files:
$ eslint .
To generate all *.js
, *.d.ts
and *.metadata.json
files:
$ yarn build
To publish on npmjs registry :
$ npm publish dist
- A proper documentation
- A proper unit tests
MIT ©2019 Cartona