3

I have been going round and round trying to find a definitive answer. Basically, I want to add the Google Analytics event tracking to a form submit - I was hoping to add an onClick to it, but I am having a tough time finding out if all the browsers support it.

I asked the person who designed the form, and she said that the reason they didn't use the button type is that is caused issues submitting the form in some browsers, but could not remember which ones.

So, will it work across browsers, or should I switch to button?

Relevant code below:

<input name="submit" id="submitme" type="image" 
  src="/graphics/creative/landing_pages/popup/ibd_signmeupbtn.gif" 
  alt="Sign Me Up!" 
  onClick="_gaq.push(['_trackEvent', 'Registrations', 'Register', 'Lightbox']);"
  tabindex="8"  />

EDIT I have _gaq defined. The real goal of this question is to make sure that using an onClick with an input image is OK to do.

0

2 Answers 2

7

Try wiring it up a little different

<input name="submit" id="submitme" type="image" 
    src="/graphics/creative/landing_pages/popup/ibd_signmeupbtn.gif" 
    alt="Sign Me Up!" 
    onclick="SignMeUp();"
    tabindex="8"  />

And create your function like so:

<script type="text/javascript">
    function SignMeUp()
    {
        if (!!_gaq) // make sure _gaq is defined
            _gaq.push(['_trackEvent', 'Registrations', 'Register', 'Lightbox']);
    }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I have the same question but wondering if this answer is still "the best" with the changes all things web in the last 5 years. Thanks!
0

you dont need to define the _gaq variable but you do need to import the google script.

here is an example of my code

var _gaq = _gaq || [];
// i'm using asp.net web forms and getting google analytics code from config
_gaq.push(['_setAccount', '<%= ConfigurationManager.AppSettings["google.analytics.code"] %>']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

4 Comments

this isn't tagged with ASP.Net
yes this is true but also, in _gaq.push(['_setAccount', 'value']); one could hard code the value or use what ever other mechanism is available to get the value. If it were php, you'll you <?php blah blah ?>. That is the only piece of this code specific to asp.net, all is general javascript.
all i was saying is that one has to import the google script to get _gaq to work.
yes, that would be an important step ;). But that code might be confusing to someone who didn't use ASP.Net. Best to stick to the tagged code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.