How can I insert two OnClick event handlers into a custom .ascx element?

Considering that I started a new job that requires the use of Umbraco and ASP.NET - platforms / languages ​​that I have been working with for only 2 days and I get a complete noob, I have a request regarding the insertion of Google Analytics event tracking code in a string the .ascx file.

Like this: I turned around on a specific line into which I need to insert the tracking code .gaq ; this is an onclick handler that corresponds to the submit button in the contact form. Line:

 <asp:LinkButton runat="server" ID="Page3NextButton" OnClick="Page3NextButton_Click" CausesValidation="true" CssClass="btnSubmit floatRight"> <span>Submit</span> </asp:LinkButton> 

I would like to add an event tracker to this OnClick property in the form of "_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])" and configure the corresponding event menu in GA. The only thing I don’t know how to add code to the existing OnClick setting is not the case of separating the two with a colon, as I could do with JavaScript elements in regular HTML (for example, onclick="blah(); dah();")

Can anyone tell me how to install two event handlers in the same OnClick property in ASP.NET ? If I sound gloomy, because this language is completely insignificant for me.

Greetings.

+6
source share
4 answers

Use OnClientClick, which is used to run JavaScript before returning the page. Sort of:

 <asp:LinkButton runat="server" ID="Page3NextButton" OnClick="Page3NextButton_Click" OnClientClick="_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])" CausesValidation="true" CssClass="btnSubmit floatRight"> <span>Submit</span> </asp:LinkButton> 

Another solution:

 Page.ClientScript.RegisterStartupScript(this.GetType(), ="_gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])", true); 

in the codebehind event handler to click. This will lead to the output of the script so that it runs when the browser loads the page.

Hope this helps!

+3
source

As you noted, you need another click event for Google Analytics.

To my knowledge, the Google Analytics event tracking code is in javascript. Thus, in this case, you can use the OnClientClick event for LinkButton to handle Google Analytics click events. as:

 <asp:LinkButton runat="server" ID="Page3NextButton" OnClick="Page3NextButton_Click" CausesValidation="true" CssClass="btnSubmit floatRight" OnClientClick="gaq.push(['_trackEvent', 'Volunteer', 'Submit', 'Volunteer Signup'])"></asp:LinkButton>. 

This should work fine!

+2
source

You can use the OnClientclick property for the button and use the Javascript function to enter the code.

0
source

Thanks to everyone and everyone. I pasted the code - now I think I'm waiting. I don’t see the category, action or shortcut displayed in my GA account, but I heard that Google Analytics takes some time to update - or does someone really need to trigger the event first to track events?

0
source

Source: https://habr.com/ru/post/947190/


All Articles