Code Monkey home page Code Monkey logo

Comments (9)

ancifer avatar ancifer commented on July 20, 2024

use

{ provide: 'googleTagManagerId', useFactory: googleTagManagerFactory, deps: [AppConfig] },
export function googleTagManagerFactory(config: AppConfig) {
return config.analytics.googleTagManagerId;
}

from angular-google-tag-manager.

monsieurPaon avatar monsieurPaon commented on July 20, 2024

What do you meen with 'AppConfig' ?

from angular-google-tag-manager.

ancifer avatar ancifer commented on July 20, 2024

What do you meen with 'AppConfig' ?

AppConfig - you app configuration with googleTagManagerId
For example:

AppModule -> providers
{
      provide: APP_INITIALIZER,
      useFactory: AppConfigurationFactory,
      deps: [AppConfig],
      multi: true,
}

export function AppConfigurationFactory(config: AppConfig) {
  return () => config.init();
}

@Injectable({
  providedIn: 'root',
})
export class AppConfig {
  googleTagManagerId: string;
  constructor(private httpClient: HttpClient) {}

  ensureInit(): Promise<AppConfig> {
    return new Promise((r) => {
      this.httpClient
        .get('/config.json', { responseType: 'text' })
        .toPromise()
        .then((res) => {
          const data = JSON.parse(res);

          this.googleTagManagerId = data.googleTagManagerId; 
          r(this);
        });
    });
  }
}

from angular-google-tag-manager.

monsieurPaon avatar monsieurPaon commented on July 20, 2024

Ho dorry okey i understand.
I have try this before but i don't understand how you link the return of the function to the tag manager module. I'm already doing something similar with an other module but in deps of the provider i can call the conf injectionToken.

Error: Google tag manager ID not provided.

from angular-google-tag-manager.

monsieurPaon avatar monsieurPaon commented on July 20, 2024
    {
      provide: APP_INITIALIZER,
      useFactory: agmConfigFactory,
      deps: [HttpClient, LAZY_MAPS_API_CONFIG],
      multi: true
    },
    {
      provide: APP_INITIALIZER,
      useFactory: AppConfigurationFactory,
      deps: [HttpClient, AppConfig],
      multi: true,
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: RequestInterceptor,
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

export function AppConfigurationFactory(config: AppConfig): any {
  return () => config.ensureInit();
}

export function HttpLoaderFactory(http: HttpClient): any {
  return new TranslateHttpLoader(http, '../assets/locales/', '.json');
}```

from angular-google-tag-manager.

tyanders1987 avatar tyanders1987 commented on July 20, 2024

Is there a way to load this module or pass it the GTM Id after the app is fully initialized?

from angular-google-tag-manager.

monsieurPaon avatar monsieurPaon commented on July 20, 2024

I think the @ancifer response is the right way, but i still lock on my error.

from angular-google-tag-manager.

ancifer avatar ancifer commented on July 20, 2024

@monsieurPaon All code. App config init run APP_INITIALIZER. After run googleTagManagerId. I hope I helped.

@Injectable({
  providedIn: 'root',
})
export class AppConfig {
  googleTagManagerId: string;
  constructor(private httpClient: HttpClient) {}

  init(): Promise<AppConfig> {
    return new Promise((r) => {
      this.httpClient
        .get<AppConfig>('/assets/config.json')
        .toPromise()
        .then((response: AppConfig) => {
          Object.assign(this, response);
          r(this);
        });
    });
  }
}

@NgModule({
  declarations: [AppComponent],
  imports: [
    GoogleTagManagerModule
  ],
  providers: [
    AppConfig,
    {
      provide: APP_INITIALIZER,
      useFactory: AppConfigurationFactory,
      deps: [AppConfig],
      multi: true,
    },
    { provide: 'googleTagManagerId', useFactory: GoogleTagManagerId, deps: [AppConfig] },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

export function AppConfigurationFactory(appConfig: AppConfig) {
  return () => appConfig.init();
}

function GoogleTagManagerId(appConfig: AppConfig) {
  return appConfig.googleTagManagerId;
}

config.json in assets

{
    googleTagManagerId: "YOU GOOGLE TAG MANAGER ID"   
}

from angular-google-tag-manager.

oajulianclementson avatar oajulianclementson commented on July 20, 2024

I've also tried the approach from @ancifer but it doesn't work for me. The googleTagManagerId factory completes before the APP_INITIALIZER promise has resolved, because of the time taken to fetch config.json.

Although Angular waits for APP_INITIALIZER promises to resolve before loading components, it doesn't wait before loading other providers, so they are loaded in parallel.

So I think that rules out using this library with a config that is loaded using APP_INITIALIZER. The only solution would be to load the config in main.ts, before boostrapping the application.

from angular-google-tag-manager.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.