0

I'm having trouble getting an OnClick event to send back to Google(Universal) Analytics.

Here's the line in question:

<a href= “link-to-asset-goes-here” onclick="ga(‘send’, ‘event’, ‘Button’, ‘Click’, ‘AllVisits_Free’, {‘NonInteraction’: 1});”>

When I inspect with Chrome Dev tools, I'm seeing this error: "Uncaught SyntaxError: Invalid or unexpected token"

When looking through other questions, I found this one:java - OnClick error

Could it possibly be just escaping all the strings then? Like so:

<a href= “link-to-asset-goes-here” onclick="ga(\‘send’\, \‘event’\, \‘Button’\, \‘Click’\, \‘AllVisits_Free’\, {\‘NonInteraction’\: 1});”>

Would that cause any issues with the NonInteraction part of the code?

Unfortunately I don't have access to test this live, and have to send each code revision over to the client each time, or I could just test different variations of code a lot easier.

And just for reference, we're using the newest version of GA(Universal) analytics tracking code. When looking at the Real Time reports in GA, it's registering that I'm on the page, but events are not firing when clicking the link.

Thanks!

2 Answers 2

0

If that it the exact code format you are using, then try using straight quotes rather than the smart quotes:

<a href= "link-to-asset-goes-here" onclick="ga('send', 'event', 'Button', 'Click', 'AllVisits_Free', {'NonInteraction': 1});">

....Something with smart quotes not playing well with JavaScript.

5
  • @nyun is there a way to force that? When looking at the code in Dev tools, the single quotes are definitely slanted, but I'm unsure how to force straight up and down quotes. Thanks!
    – Tony6D
    Commented Jan 26, 2017 at 18:44
  • Not sure what you mean by forcing it. Don't you just type it in?
    – nyuen
    Commented Jan 26, 2017 at 18:46
  • @nyun Slanted quote marks is what you're referring to as "smart quotes" correct? Again, I'm having to send the code over to a client, and they're copy/pasting from the email into the page's code editor. I'm wondering if the slanted marks are from the copy/paste.
    – Tony6D
    Commented Jan 26, 2017 at 19:17
  • There's probably some auto-formatting going on after the paste job. Simple test would be to open your dev console on the page in question, manually edit the HTML so that straight quotes are used, trigger your event, and then see if GA fires your event.
    – nyuen
    Commented Jan 26, 2017 at 19:21
  • @nyun Yes, that was it! Thank you very much for the help. Really appreciate it.
    – Tony6D
    Commented Jan 26, 2017 at 19:36
0

I was getting the same error while using this onClick code.

Straight codes worked for me. The mistake which I made was copying the code snippet from the website. Enter the quotes directly from the keyboard and it will work great!

Also for quick debugging, use the GA Debugger, it helped me resolve the error quickly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.