Skip to content

Instantly share code, notes, and snippets.

@alewolf
Last active April 22, 2022 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alewolf/efe6883b531db5fd7c46c0477e39678b to your computer and use it in GitHub Desktop.
Save alewolf/efe6883b531db5fd7c46c0477e39678b to your computer and use it in GitHub Desktop.
<?php
/**
* Track ViewItem events in Facebook using the WooCommerce Pixel Manager
* Add this to functions.php
*/
add_action('wp_footer', function (){
?>
<script>
new Promise((resolve) => {
(function waitForjQuery() {
if (window.jQuery) return resolve()
setTimeout(waitForjQuery, 100)
})()
}).then(()=>{
jQuery(document).on("wpmCategory", function () {
try {
if (!wpmDataLayer?.pixels?.facebook?.loaded) return
let eventId = wpm.getRandomEventId()
fbq("trackCustom", "ViewCategory", {}, {
eventID: eventId,
})
jQuery(document).trigger("wpmFbCapiEvent", {
event_name : "ViewCategory",
event_id : eventId,
user_data : wpm.getFbUserData(),
event_source_url: window.location.href,
})
} catch (e) {
console.error(e)
}
})
})
</script>
<?php
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment