nystudio107 / instantanalytics

DEPRECATED Instant Analytics brings full Google Analytics support to your Twig templates and automatic Craft Commerce integration with Google Enhanced Ecommerce.
Other
101 stars 4 forks source link

Event tracking gets set regardless #11

Closed terryupton closed 7 years ago

terryupton commented 7 years ago

Following on from Slack conversation. It seems the event tracking gets set regardless on where it is used. Really struggling with this. Whenever I add {% do myAnalytics.sendEvent() %} - even on an onclick onclick="{% set myAnalytics = eventAnalytics('Call to Action Footer16', contactInformation.email, url(craft.request.path), 'Footer') %} {% do myAnalytics.sendEvent() %} it seems to add an event upon pageload. Not sure if a bug or my implementation.

khalwat commented 7 years ago

@terryupton yeah the problem you're having is that Twig knows nothing about JavaScript. So when you put that code inside of the JavaScript, it's going to get executed no matter what.

When your page is rendered, the Twig template is compiled to PHP (if it wasn't already), and the code to send the event gets executed. Then the page is sent to the browser, and the onclick handler will be just empty text actually.