AdWords
4.6K members online now
4.6K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me
star_border
Reply

Syntax error when inserting onClick tag

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I was directed here by a Google Adwords Support member after a lengthy conversation in which she was eventually unable to help me with this code.

Anywho, I am attempting to add an onClick tag to an "Add to Cart" image button so that I can track the conversions of people clicking it.


This code : '<form action="https://www.landscapesupplyco.com/cart/?action=add" method="post"><input type="hidden" name="id" value="'.$id.'"><button type="submit" class="btn btn-block btn-modern">Add to <img style="max-width: 58px;" src="../img/paypal_logo_smb.png" alt="paypal" onClick="goog_report_conversion('https://www.landscapesupplyco.com/cart/')"/> cart<br/>$'.$price.'</button></form>'

is giving me a parse/syntax error and preventing the page from loading (I also tried adding a space between conversion and the beginning parentheses). Even the .pdf download link in the example located here : https://support.google.com/adwords/answer/6095821?hl=en&authuser=0 under "Setting up an onClick handler for conversions" in the image portion results in a syntax error.

What would be the correct code to avoid this error and accurately report conversions of "Add to Cart" clicks? With the given example I'd be surprised if anyone has gotten a click tracking even to report correctly, lol.


Thanks in advance for your help.

1 Expert replyverified_user

Re: Syntax error when inserting onClick tag

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭

There are a few problems here. The actual syntax error is not a javascript error coming from the onclick, its actually a PHP error. It looks like the code you pasted is probably inside of a PHP Echo command. The problem is that you're using a single quote for the url, which ends the PHP statement. You need to escape the single quote with a slash \

 

You have a <button type="submit"> in your code, but the <button> tag can not have a "type" attribute. The "type" attribute is used on <input> tags. There are 3 options depending on the visual affect you're looking for:

 

<form action="https://www.landscapesupplyco.com/cart/?action=add" method="post">

<input type="hidden" name="id" value="'.$id.'">
<input type="image" class="btn btn-block btn-modern" style="max-width: 58px;" src="../img/paypal_logo_smb.png" alt="Add to Cart" onClick="goog_report_conversion(\'https://www.landscapesupplyco.com/cart/\')"/>
</form>

 

 

<form action="https://www.landscapesupplyco.com/cart/?action=add" method="post">
<input type="hidden" name="id" value="'.$id.'">
<button class="btn btn-block btn-modern">
<img style="max-width: 58px;" src="../img/paypal_logo_smb.png" alt="paypal" onClick="goog_report_conversion(\'https://www.landscapesupplyco.com/cart/\')"/>
'.$price.'
</button>
</form>

 

 

Or ideally, it wouldn't be an "onClick" event at all, when dealing with forms, you should be using the "onSubmit" event

 

 

<form action="https://www.landscapesupplyco.com/cart/?action=add" method="post" onSubmit="goog_report_conversion(\'https://www.landscapesupplyco.com/cart/\')">
<input type="hidden" name="id" value="'.$id.'">
<input type="image" class="btn btn-block btn-modern" style="max-width: 58px;" src="../img/paypal_logo_smb.png" alt="Add to Cart" />
</form>

Re: Syntax error when inserting onClick tag

Top Contributor
# 3
Top Contributor
Good catch, Bradley. I quite agree it should be in the onsubmit for the form. I've had trouble with some (older) browsers placing it in the onclick for the image.

Pete
petebardo -- Deadhead doing AdWords