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

LVL 1
webressursAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.