Comments (9)
use
{ provide: 'googleTagManagerId', useFactory: googleTagManagerFactory, deps: [AppConfig] },
export function googleTagManagerFactory(config: AppConfig) {
return config.analytics.googleTagManagerId;
}
from angular-google-tag-manager.
What do you meen with 'AppConfig' ?
from angular-google-tag-manager.
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.
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.
{
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.
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.
I think the @ancifer response is the right way, but i still lock on my error.
from angular-google-tag-manager.
@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.
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)
- ERROR in ./node_modules/angular-google-tag-manager/fesm2015/angular-google-tag-manager.mjs 32:30-36 Can't import the named export 'Inject' from non EcmaScript module (only default export is available) HOT 8
- Dublicated tag fire
- Wrong documentation? HOT 10
- Null-Like GTM-ID should not throw an error HOT 3
- document is not defined on SSR
- Weird appendage
- Angular 15 support HOT 1
- The library does not add GTM script to the DOM HOT 3
- Push values into the data layer HOT 1
- Unable to import angular-google-tag-manager-config.service HOT 7
- Unable to resolve dependency tree - v1.6.1 tries to check v1.7.0 HOT 1
- Clear the dataLayer HOT 1
- gtm_resource_path appends to url
- Add tag at the beginning of dataLayer
- Console exception when loading GTM using the library HOT 1
- Angular 16 support HOT 7
- Youtube video keep emitting gtm.video after navigating away HOT 3
- Unable to resolve dependency tree. v1.7.0 tries to check v1.8.0
- Angular 17 Support HOT 11
- Feature request: Add removeGtmFromDom function to GoogleTagManagerService
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-google-tag-manager.