Angular Version 7: One way to include some js which is environment-specific is to
save the script at some path: E.g: src/assets/javascripts/google_analytics.js
Then in angular.json
find the build config for the specific config (Ref: here) and in the desired environment add an additional scripts array.
ng build --prod
will additionally bundle the extra script also. (whatever environment the additional script is added to taking prod for example here)
Below sample uses the production environment for demonstration but this can be done for any environment.
Sample angular.json below. Tested on Angular 7 (Please only refer to the "scripts" key inside the build config, file is heavily modified to show only the relevant part)
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"test": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {},
"configurations": {
"production": {
"scripts": ["src/assets/javascripts/google_analytics.js"]
}
}
}
}
}
},
"defaultProject": "test"
}