PluginProbe
Hostinger Tools / 2.1.8
Hostinger Tools v2.1.8
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / includes / Amplitude / Amplitude.php

Amplitude.php in Hostinger Tools 2.1.8, at includes/Amplitude/Amplitude.php

235 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hostinger\Amplitude;
4
5 use Hostinger\Config;
6 use Hostinger\Helper;
7 use Hostinger\Settings;
8 use Hostinger\Requests\Client;
9 use Hostinger\Amplitude\Actions as Amplitude_Actions;
10
11 defined( 'ABSPATH' ) || exit;
12
13 class Amplitude {
14 public const AMPLITUDE_ENDPOINT = '/v3/wordpress/plugin/trigger-event';
15 private const WOO_WIZARD_PAGE = 'page=wc-admin&path=/setup-wizard';
16 private const WOO_ONBOARDING_COMPLETED = 'woocommerce_default_onboarding_completed';
17 private const AMPLITUDE_HOME_SLUG = 'home';
18 private const AMPLITUDE_LEARN_SLUG = 'learn';
19 private const AMPLITUDE_AI_ASSISTANT_SLUG = 'ai-assistant';
20 private const AMAZON_AFFILIATE_SLUG = 'amazon_affiliate';
21 private const WOO_REQUIRED_ONBOARDING_STEPS = array( 'products', 'payments' );
22 private const WOO_ONBOARDING_TRANSIENT_REQUEST = 'woocommerce_onboarding_request';
23 private const WOO_ONBOARDING_TRANSIENT_RESPONSE = 'woocommerce_onboarding_response';
24 private const WOO_ONBOARDING_TRANSIENT_ATTEMPTS = 'woocommerce_onboarding_attempts';
25 private const WOO_ONBOARDING_STARTED_TRANSIENT_ATTEMPTS = 'woocommerce_started_attempts';
26 private const WOO_ONBOARDING_STARTED_TRANSIENT_REQUEST = 'woocommerce_started_request';
27 private const WOO_ONBOARDING_STARTED_TRANSIENT_RESPONSE = 'woocommerce_started_response';
28 private const CACHE_SIX_HOURS = 3600 * 6;
29 private const CACHE_ONE_HOUR = 3600;
30 private const CACHE_ONE_DAY = 86400;
31
32 private Config $config_handler;
33 private Client $client;
34 private Helper $helper;
35 private Settings $settings;
36
37 public function __construct() {
38 $this->helper = new Helper();
39 $this->config_handler = new Config();
40 $this->settings = new Settings();
41 $this->client = new Client(
42 $this->config_handler->get_config_value( 'base_rest_uri', HOSTINGER_REST_URI ),
43 array(
44 Config::TOKEN_HEADER => $this->helper::get_api_token(),
45 Config::DOMAIN_HEADER => $this->helper->get_host_info(),
46 )
47 );
48
49 if ( $this->helper::is_plugin_active( 'woocommerce' ) ) {
50 $this->setup_woocommerce_onboarding_events();
51 }
52 }
53
54 private function setup_woocommerce_onboarding_events(): void {
55
56 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
57 return;
58 }
59
60 if ( $this->helper->is_this_page( self::WOO_WIZARD_PAGE ) ) {
61 add_action( 'admin_init', array( $this, 'woocommerce_onboarding_started' ) );
62 }
63
64 if ( ! $this->settings->get_setting( self::WOO_ONBOARDING_COMPLETED ) ) {
65 add_action( 'admin_init', array( $this, 'woocommerce_onboarding_completed' ) );
66 }
67 }
68
69 public function send_request( string $endpoint, array $params ): array {
70 try {
71 if ( isset( $params['action'] ) && isset( $params['location'] ) && ! $this->should_send_amplitude_event( $params['action'], $params['location'] ) ) {
72 return array();
73 }
74
75 $response = $this->client->post( $endpoint, array( 'params' => $params ) );
76
77 return $response;
78 } catch ( Exception $exception ) {
79 $this->helper->error_log( 'Error sending request: ' . $exception->getMessage() );
80 }
81
82 return array();
83 }
84
85 public function should_send_amplitude_event( string $event_action, string $location ): bool {
86 $amplitude_actions = new Amplitude_Actions();
87 $one_time_per_day = array(
88 $amplitude_actions::HOME_ENTER,
89 $amplitude_actions::LEARN_ENTER,
90 $amplitude_actions::AI_ASSISTANT_ENTER,
91 $amplitude_actions::AMAZON_AFFILIATE_ENTER,
92 $amplitude_actions::WOO_ONBOARDING_STARTED,
93 $amplitude_actions::WOO_STORE_SETUP_STORE,
94 $amplitude_actions::WOO_STORE_SETUP_COMPLETED,
95 );
96
97 $event_action = sanitize_text_field( $event_action );
98
99 if ( in_array( $event_action, $one_time_per_day ) && get_transient( $event_action . '-' . $location ) ) {
100 return false;
101 }
102
103 if ( in_array( $event_action, $one_time_per_day ) ) {
104 set_transient( $event_action . '-' . $location, true, self::CACHE_ONE_DAY );
105 }
106
107 return true;
108 }
109
110 public function track_menu_action( string $event_action, string $location ): void {
111 $endpoint = self::AMPLITUDE_ENDPOINT;
112 $action = $this->map_action( $event_action );
113
114 if ( empty( $action ) ) {
115 return;
116 }
117
118 $params = array(
119 'action' => $action,
120 'location' => $location,
121 );
122
123 $this->send_request( $endpoint, $params );
124 }
125
126 public function setup_store( string $event_action ): void {
127 $amplitude_actions = new Amplitude_Actions();
128 $endpoint = self::AMPLITUDE_ENDPOINT;
129 $action = sanitize_text_field( $event_action );
130
131 if ( $amplitude_actions::WOO_STORE_SETUP_STORE !== $action ) {
132 return;
133 }
134
135 $params = array(
136 'action' => $action,
137 );
138
139 $this->send_request( $endpoint, $params );
140 }
141
142 private function map_action( string $event_action ): string {
143 $amplitude_actions = new Amplitude_Actions();
144
145 switch ( $event_action ) {
146 case self::AMPLITUDE_HOME_SLUG:
147 return $amplitude_actions::HOME_ENTER;
148 case self::AMPLITUDE_LEARN_SLUG:
149 return $amplitude_actions::LEARN_ENTER;
150 case self::AMPLITUDE_AI_ASSISTANT_SLUG;
151 return $amplitude_actions::AI_ASSISTANT_ENTER;
152 case self::AMAZON_AFFILIATE_SLUG;
153 return $amplitude_actions::AMAZON_AFFILIATE_ENTER;
154 default:
155 return '';
156 }
157 }
158
159 public function woocommerce_onboarding_started(): void {
160 $transient_response_key = self::WOO_ONBOARDING_STARTED_TRANSIENT_RESPONSE;
161 $transient_attempts_key = self::WOO_ONBOARDING_STARTED_TRANSIENT_ATTEMPTS;
162 $onboarding_started = get_transient( $transient_response_key );
163 $request_attempts = get_transient( $transient_attempts_key );
164 $amplitude_actions = new Amplitude_Actions();
165
166 if ( false === $request_attempts ) {
167 $request_attempts = 0;
168 }
169
170 if ( $onboarding_started || $request_attempts > 20 ) {
171 return;
172 }
173
174 try {
175 set_transient( self::WOO_ONBOARDING_STARTED_TRANSIENT_REQUEST, true, self::CACHE_SIX_HOURS );
176
177 if ( false === get_transient( self::WOO_ONBOARDING_STARTED_TRANSIENT_REQUEST ) ) {
178 throw new Exception( 'Unable to create transient in WordPress.' );
179 }
180
181 if ( $this->helper->is_this_page( self::WOO_WIZARD_PAGE ) ) {
182 $request = $this->send_request( self::AMPLITUDE_ENDPOINT, array( 'action' => $amplitude_actions::WOO_ONBOARDING_STARTED ) );
183
184 if ( wp_remote_retrieve_response_code( $request ) == 200 ) {
185 set_transient( $transient_response_key, true, self::CACHE_SIX_HOURS );
186 }
187 }
188
189 set_transient( $transient_response_key, 0, self::CACHE_ONE_HOUR );
190 } catch ( Exception $exception ) {
191 $this->helper->error_log( 'Error checking onboarding started: ' . $exception->getMessage() );
192 } finally {
193 set_transient( $transient_attempts_key, ++$request_attempts, self::CACHE_SIX_HOURS );
194 }
195 }
196
197 public function woocommerce_onboarding_completed(): void {
198 $transient_request_key = self::WOO_ONBOARDING_TRANSIENT_REQUEST;
199 $transient_response_key = self::WOO_ONBOARDING_TRANSIENT_RESPONSE;
200 $transient_attempts_key = self::WOO_ONBOARDING_TRANSIENT_ATTEMPTS;
201 $onboarding_completed = get_transient( $transient_response_key );
202 $request_attempts = get_transient( $transient_attempts_key ) ?? 0;
203 $amplitude_actions = new Amplitude_Actions();
204 $settings = new Settings();
205 $completed_steps = $this->helper->default_woocommerce_survey_steps_completed( self::WOO_REQUIRED_ONBOARDING_STEPS );
206
207 try {
208 if ( $onboarding_completed || $request_attempts > 20 ) {
209 return;
210 }
211
212 set_transient( $transient_request_key, true, self::CACHE_SIX_HOURS );
213
214 if ( false === get_transient( $transient_request_key ) ) {
215 throw new Exception( 'Unable to create transient in WordPress.' );
216 }
217
218 if ( $completed_steps ) {
219 $request = $this->send_request( self::AMPLITUDE_ENDPOINT, array( 'action' => $amplitude_actions::WOO_STORE_SETUP_COMPLETED ) );
220
221 if ( wp_remote_retrieve_response_code( $request ) == 200 ) {
222 set_transient( $transient_response_key, true, self::CACHE_SIX_HOURS );
223 $settings->update_setting( self::WOO_ONBOARDING_COMPLETED, true );
224 }
225
226 set_transient( $transient_response_key, 0, self::CACHE_ONE_HOUR );
227 }
228 } catch ( Exception $exception ) {
229 $this->helper->error_log( 'Error checking onboarding completion: ' . $exception->getMessage() );
230 } finally {
231 set_transient( $transient_attempts_key, ++$request_attempts, self::CACHE_SIX_HOURS );
232 }
233 }
234 }
235