| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Integrations\Other; |
| 4 |
|
| 5 |
use OPTN\Includes\Db; |
| 6 |
use OPTN\Includes\Utils\Utils; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
// TODO @samin: Implement batching. |
| 11 |
|
| 12 |
/** |
| 13 |
* Google Analytics Integration class. |
| 14 |
*/ |
| 15 |
class GoogleAnalytics { |
| 16 |
|
| 17 |
/** |
| 18 |
* Google Analytics root url |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $ga_url = 'https://www.google-analytics.com/mp/collect'; |
| 23 |
|
| 24 |
/** |
| 25 |
* DB instance |
| 26 |
* |
| 27 |
* @var Db $db |
| 28 |
*/ |
| 29 |
private $db; |
| 30 |
|
| 31 |
/** |
| 32 |
* Settings |
| 33 |
* |
| 34 |
* @var array|null |
| 35 |
*/ |
| 36 |
private $settings; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
$this->db = Db::get_instance(); |
| 43 |
$this->add_hooks(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add hooks |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function add_hooks() { |
| 52 |
add_action( 'optn_viewed_optin', array( $this, 'emit_view_event' ) ); |
| 53 |
add_action( 'optn_converted', array( $this, 'emit_conversion_event' ) ); |
| 54 |
add_action( 'optn_purchased', array( $this, 'emit_purchase_event' ) ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* View event |
| 59 |
* |
| 60 |
* @param array $data data. |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function emit_view_event( $data ) { |
| 64 |
|
| 65 |
$optin = $this->db->get_conv_by_id( $data['conv_id'] ); |
| 66 |
|
| 67 |
$event_data = array( |
| 68 |
array( |
| 69 |
'name' => 'impression', |
| 70 |
'params' => array( |
| 71 |
'optin_id' => $optin->id, |
| 72 |
'optin_title' => $optin->title, |
| 73 |
), |
| 74 |
), |
| 75 |
); |
| 76 |
|
| 77 |
$this->send_to_ga4( $event_data ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Conversion Event |
| 82 |
* |
| 83 |
* @param array $data data. |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function emit_conversion_event( $data ) { |
| 87 |
$optin = $this->db->get_optin_by_interaction_id( $data['id'] ); |
| 88 |
|
| 89 |
$event_data = array( |
| 90 |
array( |
| 91 |
'name' => 'conversion', |
| 92 |
'params' => array( |
| 93 |
'optin_id' => $optin->id, |
| 94 |
'optin_title' => $optin->title, |
| 95 |
), |
| 96 |
), |
| 97 |
); |
| 98 |
|
| 99 |
$this->send_to_ga4( $event_data ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Purchase Event |
| 104 |
* |
| 105 |
* @param array $data data. |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function emit_purchase_event( $data ) { |
| 109 |
$optin = $this->db->get_conv_by_id( $data['conv_id'] ); |
| 110 |
|
| 111 |
$event_data = array( |
| 112 |
array( |
| 113 |
'name' => 'purchase', |
| 114 |
'params' => array( |
| 115 |
'optin_id' => $optin->id, |
| 116 |
'optin_title' => $optin->title, |
| 117 |
), |
| 118 |
), |
| 119 |
); |
| 120 |
|
| 121 |
$this->send_to_ga4( $event_data ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Send event to GA4 |
| 126 |
* |
| 127 |
* @param array $events events. |
| 128 |
* @return boolean |
| 129 |
*/ |
| 130 |
private function send_to_ga4( $events ) { |
| 131 |
|
| 132 |
$settings = $this->get_settings(); |
| 133 |
$ret = true; |
| 134 |
|
| 135 |
foreach ( $settings as $setting ) { |
| 136 |
$url = add_query_arg( |
| 137 |
array( |
| 138 |
'measurement_id' => $setting['data']['measurementId'], |
| 139 |
'api_secret' => $setting['data']['apiKey'], |
| 140 |
), |
| 141 |
$this->ga_url |
| 142 |
); |
| 143 |
|
| 144 |
$payload = array( |
| 145 |
'client_id' => Utils::get_user_hash(), |
| 146 |
'events' => $events, |
| 147 |
); |
| 148 |
|
| 149 |
$resp = $this->send( $url, $payload ); |
| 150 |
|
| 151 |
if ( is_wp_error( $resp ) || 204 !== wp_remote_retrieve_response_code( $resp ) ) { |
| 152 |
$ret = false; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return $ret; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Send API Request |
| 161 |
* |
| 162 |
* @param string $url url. |
| 163 |
* @param array $payload payload. |
| 164 |
* @return array|WP_Error |
| 165 |
*/ |
| 166 |
private function send( $url, $payload ) { |
| 167 |
return wp_remote_post( |
| 168 |
$url, |
| 169 |
array( |
| 170 |
'body' => wp_json_encode( $payload ), |
| 171 |
'headers' => array( |
| 172 |
'Content-Type' => 'application/json', |
| 173 |
'User-Agent' => 'WowOptin/' . OPTN_VERSION, |
| 174 |
), |
| 175 |
'timeout' => 10, |
| 176 |
) |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get settings. |
| 182 |
* |
| 183 |
* @return array |
| 184 |
*/ |
| 185 |
private function get_settings() { |
| 186 |
if ( is_null( $this->settings ) ) { |
| 187 |
$this->settings = $this->db->get_integrations_by_type( 'ga' ); |
| 188 |
} |
| 189 |
|
| 190 |
return $this->settings; |
| 191 |
} |
| 192 |
} |
| 193 |
|