-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact-form-7-analytics.php
46 lines (38 loc) · 1.17 KB
/
contact-form-7-analytics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/*
Plugin Name: Contact Form 7 Google Analytics
Plugin URI:
Description: Enable Google Analytics Event for contact form 7
Author: Michael Gall
Version: 0.1
*/
add_action( 'init', 'WPCF7_GoogleAnalytics::init');
class WPCF7_GoogleAnalytics {
static function init() {
add_filter("wpcf7_ajax_json_echo", "WPCF7_GoogleAnalytics::json_change", 10, 2);
add_filter("wpcf7_display_message", "WPCF7_GoogleAnalytics::display_message", 10, 2);
}
static function display_message($message, $status) {
if($status == "mail_sent_ok") {
return $message . "<script type='text/javascript'>".self::js_page_event("submit")."</script>";
} else {
return $message;
}
}
static function json_change($item, $result) {
$items['onSentOk'][] = self::js_page_event("submit");
return $items;
}
static function js_page_event($pageName) {
return "(function() {
var eventLocation = document.location.pathname + '/$pageName';
if(window['_gaq']) {
_gaq.push(['_trackPageview', eventLocation]);
} else if(window['pageTracker']) {
pageTracker._trackPageview(eventLocation);
} else {
//do nothing
}
})();";
}
}