cag8f
cag8f

Reputation: 968

Adding Google Analytics tracking code to contact form

I'd like to add Google event tracking to my PHP contact form. Doing so requires me to add a particular value to the 'onsubmit' attribute of the element. My PHP file already has an 'onsubmit' attribute defined. When I delete that attribute and enter the required Google code, nothing happens when I click the 'Submit' button (i.e. form does not submit, 'Thank You' page does not load, etc).

Here is the existing PHP code:

<form

class="cpp_form"

name="<?php echo $this->prefix; ?>_pform<?php echo '_'.$this->print_counter; ?>"

id="<?php echo $this->prefix; ?>_pform<?php echo '_'.$this->print_counter; ?>"

action="<?php $this->get_site_url(); ?>" method="post" enctype="multipart/form-data"

onsubmit="return <?php echo $this->prefix; ?>_pform_doValidate<?php echo '_'.$this->print_counter; ?>(this);">

Here is the Google code I need to enter for onsubmit:

_gaq.push([‘_trackEvent’, ‘button’, ’clicked’, ’contact us’,, ’true’])

Any ideas on how to do this/what I'm doing wrong?

I've also tried entering the Google code as the value for the "onclick" attribute. When I do that, the form can successfully be submitted, but it does not show up as an 'event' in Google Analytics.

Upvotes: 0

Views: 1176

Answers (2)

CrayonViolent
CrayonViolent

Reputation: 32532

Couple of things:

1) The GA code you posted shows smart-quotes. I'm not sure whether that's just a c/p thing posting here or if that's what you used in your actual code, but smart-quotes is invalid javascript syntax.

2) There are several ways you can do this. One way is to add it to your onsubmit as you tried. You don't need to replace the current stuff in there. Just add it to it. Since your current thing in there is returning something, you will want to add it before:

onsubmit="_gaq.push(['_trackEvent', 'button', 'clicked', 'contact us',,'true']); return <?php echo $this->prefix; ?>_pform_doValidate<?php echo '_'.$this->print_counter; ?>(this);">

Alternatively, you can look for where that xxx_pform_doValidate_xxx() function is defined and put your GA code in there.

3) To be clear, you are tracking that the form submit button was clicked. In my experience, tracking form submit button clicks has little value, since it does not indicate that the form was successfully submitted, nor does it tell you why submission failed if it failed.

Upvotes: 2

jeroen
jeroen

Reputation: 91734

You could add the google code to your validation function or use a separate function where you first put the google code and then return the value of your validation function. Or add it inline before the return statement...

But as you have a "Thank You" page, you could also track that instead although that will not give you the client- and server-side unvalidated submissions.

Upvotes: 0

Related Questions