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.

Thanks in advance!

  • I don't believe wp_mail fires an action called wp_mail. You can find the function in wp-includes/pluggable.php. There are numerous filters throughout the function and I know it fires the phpmailer_init action. – Andrew Bartel Mar 6 '14 at 19:13
  • 2
    while there isn't an action named wp_mail, there is a filter, and it should run your code (though it's not really the appropriate place to do so). you can test this by putting die in your function and doing something that results in sending of mail. – Milo Mar 6 '14 at 19:19
  • wp_mail is a pluggable function, so you could just copy the whole function from the wp-includes/pluggable.php file and put than into a plugin. And then, at the very end of the wp_mail function (on your plugin), put your tracking code. – Shazzad Mar 6 '14 at 23:21

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.