最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp mail - Add a hook to wp_mail() to track Google Analytics Event

programmeradmin3浏览0评论

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
  • 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 Commented Mar 6, 2014 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 Commented Mar 6, 2014 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 Commented Mar 6, 2014 at 23:21
Add a comment  | 

1 Answer 1

Reset to default 2

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' );
发布评论

评论列表(0)

  1. 暂无评论