I have a site that leverages both Google Analytics and Google Maps. Both of these services have API keys that need to be managed in our site's code. For Google Analytics, we have two accounts, a live account and staging account to test tracking prior to lunch. For Google Maps we actually need a unique API key for every single host name. We have several staging domains and local hostnames so our maps code has quite a bit of toggling for API keys.
Right now I am managing my keys in a C# code-behind for the respective pages using a switch on the Request.Url.Host
:
// for analytics in file1
switch (Request.Url.Host) {
case "staging": ltlUACode.Text = "stageKey"; break;
case "client.stage.com": ltlUACode.Text = "stageKey"; break;
case "www.livesite.com": ltlUACode.Text = "liveKey"; break;
case "livesite.com": ltlUACode.Text = "liveKey"; break;
}
// for maps in file2
switch(Request.Url.Host) {
case "staging": GoogleMapsKey = "uniqueKey1"; break;
case "client.stage.com": GoogleMapsKey = "uniqueKey2"; break;
case "www.livesite.com": GoogleMapsKey = "uniqueKey3"; break;
// etc
}
I'd like to know what would a better method be to keep track of all of these keys. I considered two possible things that could work:
- Using app settings in my
web.config
: one issue here though is that the maps keys change often based on the hostnames (i.e. the many variants for the maps key would be a nightmare to manage) - Having a C# class to handle all of these keys based on the hostname: I could write a static class that has properties for the analytics key and the maps key and based on the
Request.Url.Host
I could set the properties to their appropriate values. This method is essentially my switch method wrapped up into a helper class.