kolkov / ngx-metrika

Angular Yandex Metrika (Модуль поддержки счетчиков Яндекс Метрика для Angular 6+)
MIT License
14 stars 4 forks source link

Allow deferred configuring #2

Closed un-def closed 5 years ago

un-def commented 5 years ago

It seems like NgxMetrikaService.configure() could be used for deferred configuring but it does not actually work that way:

// app.module.ts

@NgModule({
  imports: [
    ...
    // Omit config parameter and configure later
    NgxMetrikaModule.forRoot(),
  ]
})
// app.component.ts

export class AppComponent implements OnInit {

  constructor(
    private ym: NgxMetrikaService,
    // Backend API service
    private api: MyApiService,
  ) {}

    ngOnInit() {
      // Fetch the config from the server and configure metrika
      // MyApiService.getYandexMetrikaConfig() returns the following object:
      // { id: ..., defer: true, webvisor: true, clickmap: true }
      this.api.getYandexMetrikaConfig().subscribe(config => this.ym.configure(config));
    }

The example above doesn't work properly due to following reasons:

  1. configure() does not set default parameters (triggerEvent: true, trackPageViews: true) as the constructor does. You should set them explicitly in the config object.
  2. configure() does not set this.config but onHit() and onReachGoal() need it.

I've done slight refactoring in this PR, so it is now possible to configure NgxMetrikaService as in the example above.

kolkov commented 5 years ago

Hi! I moved lib files to separate project. Update PR please.

codecov-io commented 5 years ago

Codecov Report

Merging #2 into master will not change coverage. The diff coverage is 75%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master       #2   +/-   ##
=======================================
  Coverage   47.14%   47.14%           
=======================================
  Files           3        3           
  Lines          70       70           
  Branches        9        9           
=======================================
  Hits           33       33           
  Misses         34       34           
  Partials        3        3
Impacted Files Coverage Δ
.../kolkov/ngx-metrika/src/lib/ngx-metrika.service.ts 43.93% <75%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 34aa57e...cc8b0d1. Read the comment docs.

un-def commented 5 years ago

Done.

kolkov commented 5 years ago

Thanks!

kolkov commented 5 years ago

I was change folder structure after merge, check please.

un-def commented 5 years ago

Unfortunately, I no longer have access to the project where this library has been used, therefore I cannot recheck the library code again. But since you just moved/renamed a directory, it doesn't seem to be a problem.

kolkov commented 5 years ago

Hi! Thanks for your response! No problem.)