Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dynamically set App Key for SPA

New Poster

My company builds an Atomic SPA JavaScript application, compiled once, deployed for any Environment (DEV, QA, PROD). Because the same piece of code is used for DEV, QA and PROD we cannot have a Static AppKey. We have multiple AppDynamics accounts, one for each environment. 


So, how do we configure the SPA JavaScript application at runtime? Simple we call a REST service at a "local" url of : /spa/config and it returns the appropiate configuration for that environment. 


Note: We don't want to compile a JS app for every environment, we like having one build that can be deployed anywhere. 


So that doesn't work with AppDynamics because you must put the AppKey in the header of your index file. 


What I would like to happen: 


1. add AppDynamic snippit to index.html header. do all the initial loading and setup but don't transfer any data back to AppDynamic central, just store it. 

2. SPA finished loading and calls /spa/config REST service to pull config, including EUM_APP_KEY. 

2. Once EUM_APP_KEY is set, start transmitting data, both stored and live. 


Is this currently possible? If not, is it possible to update current scripts? 



AppDynamics Team (Retired)

Hi Taylor,


Currently we don't support modifying our JS agent files as it can lead to unexpected errrors on the page and can even effect the monitoring as well.


From your use case I can understand that for all your three environments, i.e. Dev, QA and Prod there is a need to get JS agents with different app keys, so instead of doing that at runtime, can you just use the same agent code with different app key as described in our manual injection piece and then see if that works?


You can even customize each JS agent for different environment as mentioned below:


Let us know is this helps.




I was more concerned with the Lifecycle and when the JS agent was initialized.

In general, it's recommended to put it in the index.html file's header so it's loaded first. If we dynamically set the app_key it will need to be initialized later in the lifecycle.

1. Index.html
2. JS Loaded
3. SPA calls REST config service to pull ENV parameters
4. Initialize AppDynamic JS Agent with app_key

As I understand we will lose some loading metrics if we don't initialize the Agent in the index.html header.

We can try it and see what happens.

AppDynamics Team (Retired)

Hi Taylor,


Yes, your understanding is correct and you might miss few metrics if JS agent is not loaded/initialized early on the application page.


Let us know if you have any further queries around this.