Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1177
  • Last Modified:

Run Google Analytics tracking code from ASP.NET code behind file (.cs)

Hi!

Is it possible to run a Google Analytics tracking code from ASP.NET code behind (default.aspx.cs)?

Here is the Google tracking script:
<script>
$j = jQuery.noConflict();
$j(function () {
analytics.track('Paid User created');
});
</script>

Open in new window


This script above shall only run in some cases. That's why I need to run it from the code behind file:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        {
            if (SecurePayment == true)
            {
                PayWithSecure();
            }
            else
            {
                PayWithoutSecure();
            }
        }
    }

    protected void PayWithSecure()
    {
        if (someting)
        {
            //RUN GOOGLE TACKING CODE
        }
        else
        {
            //....
        }
    }

    protected void PayWithoutSecure()
    {
        if (someting)
        {
            //RUN GOOGLE TACKING CODE
        }
        else
        {
            //....
        }
    }

}

Open in new window

0
webressurs
Asked:
webressurs
1 Solution
 
Randy PooleCommented:
Change your js to:
<script>
function trackanalytics()
{
$j = jQuery.noConflict();
$j(function () {
analytics.track('Paid User created');
});
}
</script>

Open in new window

Then on your code behind call this when you need to run the js/
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "track", "trackanalytics();", true);

Open in new window

0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now