-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversion_Tracking.php
More file actions
207 lines (181 loc) · 6.26 KB
/
Copy pathConversion_Tracking.php
File metadata and controls
207 lines (181 loc) · 6.26 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Class Google\Site_Kit\Core\Conversion_Tracking
*
* @package Google\Site_Kit\Core\Modules
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Core\Conversion_Tracking;
use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\Contact_Form_7;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\Easy_Digital_Downloads;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\Mailchimp;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\Ninja_Forms;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\OptinMonster;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\PopupMaker;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\WooCommerce;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Event_Providers\WPForms;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Tags\GTag;
use LogicException;
/**
* Class for managing conversion tracking.
*
* @since 1.126.0
* @access private
* @ignore
*/
class Conversion_Tracking {
/**
* Context object.
*
* @var Context
*/
private $context;
/**
* Conversion_Tracking_Settings instance.
*
* @since 1.127.0
* @var Conversion_Tracking_Settings
*/
protected $conversion_tracking_settings;
/**
* REST_Conversion_Tracking_Controller instance.
*
* @since 1.127.0
* @var REST_Conversion_Tracking_Controller
*/
protected $rest_conversion_tracking_controller;
/**
* Supported conversion event providers.
*
* @since 1.126.0
* @since 1.130.0 Added Ninja Forms class.
* @var array
*/
public static $providers = array(
Contact_Form_7::CONVERSION_EVENT_PROVIDER_SLUG => Contact_Form_7::class,
Easy_Digital_Downloads::CONVERSION_EVENT_PROVIDER_SLUG => Easy_Digital_Downloads::class,
Mailchimp::CONVERSION_EVENT_PROVIDER_SLUG => Mailchimp::class,
Ninja_Forms::CONVERSION_EVENT_PROVIDER_SLUG => Ninja_Forms::class,
OptinMonster::CONVERSION_EVENT_PROVIDER_SLUG => OptinMonster::class,
PopupMaker::CONVERSION_EVENT_PROVIDER_SLUG => PopupMaker::class,
WooCommerce::CONVERSION_EVENT_PROVIDER_SLUG => WooCommerce::class,
WPForms::CONVERSION_EVENT_PROVIDER_SLUG => WPForms::class,
);
/**
* Constructor.
*
* @since 1.126.0
*
* @param Context $context Plugin context.
* @param Options $options Optional. Option API instance. Default is a new instance.
*/
public function __construct( Context $context, Options $options = null ) {
$this->context = $context;
$options = $options ?: new Options( $context );
$this->conversion_tracking_settings = new Conversion_Tracking_Settings( $options );
$this->rest_conversion_tracking_controller = new REST_Conversion_Tracking_Controller( $this->conversion_tracking_settings );
}
/**
* Registers the class functionality.
*
* @since 1.126.0
*/
public function register() {
$this->conversion_tracking_settings->register();
$this->rest_conversion_tracking_controller->register();
add_action( 'wp_enqueue_scripts', fn () => $this->maybe_enqueue_scripts(), 30 );
$active_providers = $this->get_active_providers();
array_walk(
$active_providers,
function ( Conversion_Events_Provider $active_provider ) {
$active_provider->register_hooks();
}
);
}
/**
* Enqueues conversion tracking scripts if conditions are satisfied.
*/
protected function maybe_enqueue_scripts() {
if (
// Do nothing if neither Ads nor Analytics *web* snippet has been inserted.
! ( did_action( 'googlesitekit_ads_init_tag' ) || did_action( 'googlesitekit_analytics-4_init_tag' ) )
|| ! $this->conversion_tracking_settings->is_conversion_tracking_enabled()
) {
return;
}
$active_providers = $this->get_active_providers();
array_walk(
$active_providers,
function ( Conversion_Events_Provider $active_provider ) {
$script_asset = $active_provider->register_script();
$script_asset->enqueue();
}
);
$gtag_event = '
window._googlesitekit = window._googlesitekit || {};
window._googlesitekit.throttledEvents = [];
window._googlesitekit.gtagEvent = (name, data) => {
var key = JSON.stringify( { name, data } );
if ( !! window._googlesitekit.throttledEvents[ key ] ) {
return;
}
window._googlesitekit.throttledEvents[ key ] = true;
setTimeout( () => {
delete window._googlesitekit.throttledEvents[ key ];
}, 5 );
gtag( "event", name, { ...data, event_source: "site-kit" } );
}
';
wp_add_inline_script( GTag::HANDLE, preg_replace( '/\s+/', ' ', $gtag_event ) );
}
/**
* Gets the instances of active conversion event providers.
*
* @since 1.126.0
*
* @return array List of active Conversion_Events_Provider instances.
* @throws LogicException Thrown if an invalid conversion event provider class name is provided.
*/
public function get_active_providers() {
$active_providers = array();
foreach ( self::$providers as $provider_slug => $provider_class ) {
if ( ! is_string( $provider_class ) || ! $provider_class ) {
throw new LogicException(
sprintf(
/* translators: %s: provider slug */
__( 'A conversion event provider class name is required to instantiate a provider: %s', 'google-site-kit' ),
$provider_slug
)
);
}
if ( ! class_exists( $provider_class ) ) {
throw new LogicException(
sprintf(
/* translators: %s: provider classname */
__( "The '%s' class does not exist", 'google-site-kit' ),
$provider_class
)
);
}
if ( ! is_subclass_of( $provider_class, Conversion_Events_Provider::class ) ) {
throw new LogicException(
sprintf(
/* translators: 1: provider classname 2: Conversion_Events_Provider classname */
__( "The '%1\$s' class must extend the base conversion event provider class: %2\$s", 'google-site-kit' ),
$provider_class,
Conversion_Events_Provider::class
)
);
}
$instance = new $provider_class( $this->context );
if ( $instance->is_active() ) {
$active_providers[ $provider_slug ] = $instance;
}
}
return $active_providers;
}
}