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 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!
Share Improve this question asked Mar 6, 2014 at 18:47 garysgarys 111 bronze badge 3 |1 Answer
Reset to default 2I 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' );
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 puttingdie
in your function and doing something that results in sending of mail. – Milo Commented Mar 6, 2014 at 19:19wp_mail
is a pluggable function, so you could just copy the whole function from thewp-includes/pluggable.php
file and put than into a plugin. And then, at the very end of thewp_mail
function (on your plugin), put your tracking code. – Shazzad Commented Mar 6, 2014 at 23:21