-3

Im sure there is a simple explanation to this but I cant find an answer to this anywhere. This is a function that is called on the header to display phone number. Im trying to add the click-to-call tracking to this but when I add it, it breaks the site. So

This is the current code:

function themeblvd_header_contact() {
  $link_address = 'tel:3026560214';
  echo "<div class='contact-number'>Contact us at <a href='$link_address'>(302) 656-0214</a></div>";
}

When I add the click-to-call is when it breaks:

$link_address = 'tel:3026560214'; 
echo "<div class='contact-number'>Contact us at <a onclick="ga('send', 'event',
'phone number', 'click to call', '3026560214', 1);" href='$link_address'>(302) 656-0214</a></div>";
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119

1 Answers1

1

I see that you are closing the string in the middle of it and then making a new one, two quotes to much is probably the problem here. try to escape the string like this.

$link_address = 'tel:3026560214'; 
echo "<div class='contact-number'>Contact us at <a onclick=\"ga('send', 'event',
'phone number', 'click to call', '3026560214', 1);\" href='$link_address'>(302) 656-0214</a></div>";
JimboSlice
  • 692
  • 4
  • 13
  • Thanks! I think that did it. So just for clarification if you don't mind, why is the forward slash before quotation at the end of the onclick? – Angel Herrera Apr 22 '15 at 16:20
  • @AngelHerrera Because the `"` would end your string without being escaped with a `\`. – ceejayoz Apr 22 '15 at 16:22
  • You start your string with a double qoute, right. the `\"`basically says that the quoute is a part of the string. otherwise it would close the string that you pass to the echo. if that make more sense? – JimboSlice Apr 22 '15 at 16:29