charliebreckn Posted October 14, 2023 Posted October 14, 2023 Trying to get Facebook to track page view + addtocart function when buttons are clicked on pages so that I can track that as consideration. I have added the base meta pixel code to the website tools - code injection. (replaced my actual pixel ID with "mypixelid") <!-- Meta Pixel Code --> <script> !function(f,b,e,v,n,t,s) {if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)}; if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; n.queue=[];t=b.createElement(e);t.async=!0; t.src=v;s=b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t,s)}(window, document,'script', 'https://connect.facebook.net/en_US/fbevents.js'); fbq('init', 'mypixelid'); fbq('track', 'PageView'); </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1389419488569187&ev=PageView&noscript=1" /></noscript> <!-- End Meta Pixel Code --> Then added the following code under advanced > inject custom CSS to pages where I want button clicks to be tracked as "addtocart" so I can retarget etc. <script> function fpTrackingClicks() { // Add to Cart var btns = document.querySelectorAll('.sqs-block-button-element'); if (btns) { for (var i = 0; i < btns.length; i++) { btns[i].addEventListener('click', function() { fbq('track', 'AddToCart'); }, false); } } } </script> For some reason, the code isn't working properly/ it's not registering through Facebook. Any idea to what the issue is? Thank you in advance!
tuanphan Posted October 16, 2023 Posted October 16, 2023 Try replace this line var btns = document.querySelectorAll('.sqs-block-button-element'); with this var btns = document.querySelectorAll('.sqs-add-to-cart-button-wrapper'); Email me if you have need any help (free, of course.). Answer within 24 hours. Or send to forum message Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment