Friday, January 25, 2008

How to include Adwords Conversion Code in your .NET Masterpage but only display on your receipt page - C#

The following files are updated:
MasterPage.master.cs - (where "MasterPage" is the name of your master page file)
MasterPage.master - (where "MasterPage" is the name of your master page file)
ReceiptPage.aspx.cs - (where "ReceiptPage" is the name of your receipt page file)
ReceiptPage.aspx - (where "ReceiptPage" is the name of your receipt page file)

Step 1 - MasterPage.master.cs:
We will be adding an integer variable to the “public partial class MasterPage :
System.Web.UI.MasterPage" class:

public partial class MasterPageNew : System.Web.UI.MasterPage

//Adding int variable to control display of Google Adwords Code
//Receipt page will change variable to allow for display
public int AdwordsConversion = 0;

This variable will be the trigger to show/hide the Adwords conversion on your Masterpage. This is step 1 as the creation of this variable will not disrupt any other code on your site. For now it’s just a new variable that has been initialized but not being used.

Step 2 - MasterPage.master:
Since we want this to be the last thing displayed before the tag in our HTML, we need it to reside on the Masterpage but only display when we want it. This block of code handles this:

<% if(AdwordsConversion == 1) { %>

<!-- Google Code for purchase Conversion Page -->
<script language="JavaScript" type="text/javascript">
var google_conversion_id = YOURIDHERE;
var google_conversion_language = "en_US";
var google_conversion_format = "1";
var google_conversion_color = "FFFFFF";
if (1.0) {
var google_conversion_value = 1.0;
var google_conversion_label = "purchase";
<script language="JavaScript" src="">
<img height=1 width=1 border=0
src=" ">

<% } %>

Step 3 - ReceiptPage.aspx:
Now that the MasterPage has a variable for us to manipulate, we need to be able to change it from a content page. Adding a page directive will accomplish this:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

This should be placed immediately following your main page directive (Page Language=”C#” etc etc). Adding this line makes getting to MasterPage variables easy, though it can be accomplish in other ways.

Step 4 - ReceiptPage.aspx.cs:
Now that the other steps are in place, we add the code to the ReceiptPage to change the value of AdwordsConversion. Since this variable resides on the Masterpage we have to reference it correctly as shown below. We placed ours in the Page_Load:

protected void Page_Load(object sender, EventArgs e)
//Increasing value of AdwordsConversion variable to allow Adwords code display only on ReceiptPage
Master.AdwordsConversion = 1;

Once your ReceiptPage loads it changes the value of AdwordsConversion to 1. Looking back to Step 2, the code will only display if AdwordsConversion == 1.

Leaving the ReceiptPage will cause the value to return to 0 on the next page you go to, thus making the code hidden again. This happens since the content page is loaded after the MasterPage sets the AdwordsConversion to 0, thus setting the value to 1 and displaying your code only for the Receipt Page.

This section can also be used to house the UTM transaction data for submission to Google Analytics. Another post on that is to come.