Last active
May 29, 2020 22:15
-
-
Save wpmudev-sls/5fc34beaee5a34947cda67ccc6a56bff to your computer and use it in GitHub Desktop.
[Forminator Pro] - Google Analytics & Facebook Pixel Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] - Google Analytics & Facebook Pixel Integration | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Send custom events to Google Analytics & Facebook (as of 1.12.1.1) | |
* Author: Alessandro Kaounas @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* Task: 0/11289012348292/1175295834728851 | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
// No need to do anything if the request is via WP-CLI. | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
add_action( 'wp_footer', function(){ | |
/** | |
* Tracking ID and property number | |
* | |
* The tracking ID is a string like UA-000000-2. | |
* It must be included in your tracking code to tell Analytics which account and property to send data to. | |
* Help: https://support.google.com/analytics/answer/7372977 | |
*/ | |
$tracking_id = 'UA-XXXXXXXX-X'; | |
/** | |
* Facebook Pixel ID | |
* | |
* Help: https://www.facebook.com/ads/manager/pixel/facebook_pixel | |
*/ | |
$facebook_pixel = 'XXXXXXXX'; | |
/** | |
* Default: true | |
* Set to 'false' if another plugin loads Google Analytics & Facebook Pixel tracking code property. | |
*/ | |
$tracking_code = true; | |
?> | |
<?php if( $tracking_code ) { ?> | |
<!-- Global site tag (gtag.js) - Google Analytics --> | |
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo $tracking_id; ?>"></script> | |
<script> | |
window.dataLayer = window.dataLayer || []; | |
function gtag(){ dataLayer.push( arguments ); } | |
gtag( 'js', new Date() ); | |
gtag( 'config', '<?php echo $tracking_id; ?>' ); | |
</script> | |
<!-- Facebook 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', '<?php echo $facebook_pixel; ?>'); | |
fbq('track', 'PageView'); | |
</script> | |
<noscript> | |
<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id={your-pixel-id-goes-here}&ev=PageView&noscript=1"/> | |
</noscript> | |
<!-- End Facebook Pixel Code --> | |
<?php } ?> | |
<?php | |
/** | |
* Forminator GA Integration | |
*/ ?> | |
<script type="text/javascript"> | |
(function($){ | |
$(document).on( 'forminator:form:submit:success', function( formData ){ | |
var form_id = $(formData.target).find('input[name="form_id"]').val(); | |
if( form_id == 210 ){ | |
// Do something here... | |
} | |
if ( typeof window['gtag'] == 'function' ) { | |
<?php | |
/** | |
* Measure Google Analytics Events | |
* https://developers.google.com/analytics/devguides/collection/gtagjs | |
*/ ?> | |
gtag( 'event', 'action', { 'event_category': 'category', 'event_label': 'label','value': 'value' } ); | |
}else{ | |
console.log( 'Google Analytics not detected. Aborted sending event...' ); | |
} | |
if ( typeof window['fbq'] == 'function' ) { | |
<?php | |
/** | |
* Facebook Pixel Event | |
*/ ?> | |
fbq( 'track', 'Purchase', { currency: "USD", value: 30.00 } ); | |
}else{ | |
console.log( 'Facebook Pixel not detected. Aborted sending event...' ); | |
} | |
}); | |
})(jQuery); | |
</script> | |
<?php }, 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment