1.2.0 • Published 3 months ago

@cartona/ngx-analytics-next v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

ngx-analytics-next

An Angular module wrapper for Segment's official analytics-next JS library.

It is inspired by @opendecide/ngx-segment-analytic

Installation

To install this library, run:

$ yarn add ngx-analytics-next

Using Segment

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 '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 '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"));
  }

}

API

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;

Development

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

TODO

  • A proper documentation
  • A proper unit tests

License

MIT ©2019 Cartona

1.2.0

3 months ago

1.2.0-beta

3 months ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago