Hello All,

i am currently using Google Analytics to collects stats about my site ( my site is created in ASP.NET 2.0 and C#).

i have asp:imagebutton called "Confirm Order". i want to track the clicking of this button.
the code for this button is as follows:

void ibConfirm_Click( object sender, ImageClickEventArgs e )
    {
        try
        {
            if ( Cart.Order.Items.Length == 0 )
            {
                Mysite.ClearBasket( );
                Response.Redirect( string.Concat(
                    ConfigurationManager.AppSettings[ "HTTPDirectory" ],
                    "cart.aspx?sto=true" ) );
            }
        
        
        Cart.Order.Purchase( );
                                
        sendConfirmationEmail( Cart.Order );
      
        Mysite.ClearBasket();

        Server.Transfer("thanks.aspx", false);
       }
        catch (Exception ex)
        {
        this.HandlePageError(ex);
        }
        
    }

my question how can i add the GA tracking script to this button click in the code behind.

my GA tracking Script is like below:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
pageTracker._trackEvent('confirmOrder', 'Submit');
} catch(err) {}</script>

Thanks and i really appreciate any help.

I want to know how i can add this script to the onclick code above to so i can track the button clicks.

Thanks

Recommended Answers

All 6 Replies

Keep the analytics script on your page as you would otherwise have it. Then you can use the OnClientClick attribute of the ImageButton and make a google analytics JS call from there. We typically use that when a form submits offsite, for example. We want to track users as they leave. The same could be done for your image button.

Something like

<asp:ImageButton 
     ID="imageButton" 
     runat="server" 
     ImageUrl="whatever.jpg" 
     OnClientClick="javascript:pageTracker._trackPageview('/outgoing/nl_signup');" />

This example would create a hit in GA on a /outgoing/nl_signup page, a page that does not actually exist.

commented: thanks a lot +0

thanks for replying, i have a question which might sound silly , now i am already implementing onclick of the image button programmatically from code behind.

Is it safe to add onclientclick also to the Imagebutton as you have shown in the example sir.

the reason for my concern is this ImageButton is in final step of the shopping cart process and its in a secured ( https) mode.

Thanks any advice is highly appreciated

You can have both events. OnClick would be the server event, OnClientClick is the client-side event. The two are not related, except for the fact that clicking the button will generate both events. Naturally, the client would do its processing before the postback makes it to the server.

ok thank you now is it possible to add pageTracker._trackEvent instead of pageTracker._trackPageview

Thanks

You can add whatever is legal in terms of a Analytics call. Just have the appropriate GA scripts already on the page and then you can tie into them with client-side events that occur with button clicks, form submissions, etc.

thanks for help. i have successfully added the tracking code. appreciate it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.