0

I am trying to put in scripts for google analytics in my code. But I want it to run only if the environment is production and not in dev or qa. This javascript runs at page load itself and I am confused how to make it run conditionally. My app is an asp.net app.

Javascript is always confusing to me. There are many other javascripts in the page and I just need to disable this one. Had it been a .net code/function I would check for the environment and conditionally run the function. But I am confused about how to accomplish this type of functionality with javascript

1
  • 'Javascript is always confusing to me' Ohhh! .NET breaking web development page by page! Commented Jan 18, 2011 at 19:44

4 Answers 4

1

it depends a lot what it does, and how you have your JS architecture done in your site.

For example, in my site I have this commons.js file that is loaded in every page of the site, as this behavior could be useful in other code, i'll have this global variable called "DEBUG"

For production:

var DEBUG = false;

For development and QA:

var DEBUG = true;

and in you function something like:

if (!DEBUG) {
   // DO YOUR THING...

}

You can also, have this variable (DEBUG) done dynamically depending on some logic in your controller layer.

Sign up to request clarification or add additional context in comments.

Comments

1

Put your GA code in a control. Put that control on your Master Page. Sniff the IP of the server. If it's not production, "hide" the control on that page. .NET can do it all by itself. No need for "confusing" javascript.

Comments

1

I don't know about ASP but you can include html script tag conditionally

var DEVEL = true;

if (!DEVEL) {
   #call  echo function or whatever ASP have
   echo "<script <!-- put code for google analytics here -->";
}

Comments

1

Look at the location object: http://www.w3schools.com/jsref/obj_location.asp. You can put in a script that runs on page load: if (location.host == "yourproductionhostname"){yourfunction();}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.