title: How to Inject HTML on ASP.NET MVC master page
body: <p>I have a simple, but probably common problem on how to inject HTML inside an ASP.NET MVC master page. I have a google analytics tracking code that sits on my master page. The code looks like this:</p>
  
  <pre><code>var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-1']);
  _gaq.push(['_trackPageview']);
  // need to inject ecommerce code here
  (function () {
      // google analytics code here
  })();
  </code></pre>
  
  <p>I'm using ecommerce tracking and I want to inject the "cart" information in side this HTML on the receipt page (and ONLY on this page). So I do something like this:</p>
  
  <pre><code>var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-1']);
  _gaq.push(['_trackPageview']);
  &lt;% if(ViewData["googleanalytics"]!=null) {%&gt;
  &lt;%= ViewData["googleanalytics"] %&gt;
  &lt;% } %&gt;
  (function () {
      // google analytics code here
  })();
  </code></pre>
  
  <p>Then in the controller, I have code that looks like this:</p>
  
  <pre><code>[HttpGet]
  public ActionResult Receipt()
  {
      var receipt = // get receipt model
  
      // get google analytics javascript. This function pulls
      // the data from the receipt model
      ViewData["googleanalytics"] = GetAnalyticsInfo(receipt); 
  
      return View(receipt);
  }
  </code></pre>
  
  <p>This whole thing seems like a little bit of a kludge and I was wondering if anybody had better ideas to handle this situation?</p>