Add a hook to wp_mail() to track Google Analytics Event

I hope you’ll be able to help me on this one:
I’m trying to trigger a GA event for every email sent via a WordPress site.
My idea was to add the following code to my child theme’s functions.php file:

add_action("wp_mail", "trigger_contact_event");
function trigger_contact_event(){
include "include/Galvanize.php"; //Galvanize is a php class able to trigger GA events server-side
$GA = new Galvanize('UA-XXXXXXX-XX');
$GA->trackEvent("Contact", "info request");
}

But unfortunately, this doesn’t trigger anything.
Would anyone have a solution? Even another way to trigger a GA event, as long as it is hooked to any email sent via the wordpress website.

Read More

Thanks in advance!

Related posts

1 comment

  1. I would use phpmailer_init (unless you’re using a plugin that overrides wp_mail):

    function trigger_contact_event( $phpmailer ) {
        // See PHPMailer class for available properties & methods if you need
        // information about the email being sent.
    }
    
    add_action( 'phpmailer_init', 'trigger_contact_event' );
    

Comments are closed.