digitalwithyou / craft-facebook-conversion

Craft CMS plugin to send web events directly to Facebook
https://digitalwithyou.com/en/plugins/facebook-conversion/getting-started
Other
0 stars 3 forks source link

Call Track inside javascript event #35

Closed dellanesta closed 1 year ago

dellanesta commented 1 year ago

hi @Diewy excuse me but i can't reopen the issue. I try to implement a custom event inside a click event and send the Server event immediatly and the client event correctly after the click... This is my code:

{% js%}
document.getElementById("testbutton").addEventListener("click", function() {
  //Client event      
  fbq('track', 'clickEvent', {  content_name: 'Click'},{eventID:'123456789' });

 //Server event
 {{ fbEvent('clickEvent',{},{'content_name': 'Click'},'123456789') }}
})
{% endjs %}

Could you please send us a working example to implement tha? Thank you

Diewy commented 1 year ago

Hi,

Since I'm following the issue, I'll get notified when you reply to it, even when it's closed. So there is no need to reopen or create new issues.

It seems like you're mixing Client with Server side code. The javascript needs to be runned by the client, your browser.

It can be done via the same approach as for Cached Pages. Create a template with only the server event and fetch it via javascript when needed.

Something like this:

/templates/events/click.html.twig

{% set eventId = craft.request.getQueryString('eventid') %}

{% if eventId %}
  {{ fbEvent('clickEvent', {}, {'content_name': 'Click'}, eventId) }}
{% endif %}

Your javascript:

document.getElementById("testbutton").addEventListener("click", function() {
  // Client event      
  fbq('track', 'clickEvent', {  content_name: 'Click'},{eventID:'123456789' });

 // Server event
 fetch('https://yoururl.com/events/click?eventid=123456789');
})

Please note that I haven't tested the code. Its functionality is highly dependent on the rest of your setup, and while it might work, it is not a default supported feature of the plugin.

dellanesta commented 1 year ago

ok thank you