0

I want to use an event goal onclick for tag and the goal I used doesn't work

<a onClick="ga('send', 'event', 'Captain', 'Click', 'Captain telegram', '1');" href="http://1go.ir/FUSeN" target="_blank"><img src="https://akhbarejadid.com/wp-content/uploads/2020/11/capitanever.gif" width="100%"></a>

I set event goal in google analytics as below :
Category : Captain
Action : Click
Label : Captain telegram
Value : 1

2
  • It doesn't work because clicking on a link unloads the page. It may fire the onclick event but that code doesn't have time to contact the GA server with the data before the visitor leaves the page. Dec 2, 2020 at 11:57
  • @StephenOstermiller So what do you suggest for external link goals? Dec 2, 2020 at 12:07

1 Answer 1

2

The event value is an integer and should not be within quote characters.

ga('send', 'event', 'Captain', 'Click', 'Captain telegram', 1);

https://developers.google.com/analytics/devguides/collection/analyticsjs/events#event_fields

As the link is using target="_blank", the visitor leaving the page should not be an issue as the initial page with the link itself doesn't refresh or load a new page, so the event hit should not be interrupted.

If for some reason it is an issue, to avoid the need for using a hitCallback function, the default transport mechanism can be changed from image to beacon. eg:

ga('send', 'event', 'Captain', 'Click', 'Captain telegram', 1, {transport: 'beacon'});  

If the user's browser does not support 'navigator.sendBeacon' method, it will fall back to 'image'.

https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#specifying_different_transport_mechanisms

I set event goal in google analytics as below :
Category : Captain Action : Click
Label : Captain telegram
Value : 1

Regarding the goal itself not working, that would possibly be due to the goal looking for a value of 1, yet the onclick event currently configured with the value contained within single quotes.

Also, when testing goals, clear cookies or use an incognito window after each test to end current session. A goal will only report a conversion once within the same session, so subsequent tests within the same session will appear as the goal not working.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.