Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save patrickfreitasdev/46c9dbf946b3cc34a46ae3a24d58635b to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/46c9dbf946b3cc34a46ae3a24d58635b to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: [Forminator Pro] - Google Analytics
* Plugin URI: https://premium.wpmudev.org/
* Description: Send custom events to Google Analytics Only (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 = 'G-DL19VSBNJ4';
/**
* 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>
<?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...' );
}
});
})(jQuery);
</script>
<?php }, 99 );
@whynotadv
Copy link

whynotadv commented Sep 13, 2023

Any chance you could help me fix my code? What About Google Tag Manager? Can you show how to implement that as well? Your code is odd to me because the URL shows Google Tag Manager reference but the title says GA or Google Analytics. Are they two separate analytic platforms?? Anyway, Here's my code, I'm trying to connect Google Analytics to create a custom event and also send the data to Google Tag Manager. I'm not using Facebook pixel so how do I comment that out/separate it without killing the code? Here's a link to the text file. I can't figure out how to get the code pasted into this WYSIWYG editor without it looking like garbage.

https://drive.google.com/file/d/1rkwgC1PYYK2y5YMLHHvUK-iP-ub1EmTc/view?usp=sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment