| @@ -14,9 +14,10 @@ | ||
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | 16 | namespace WCPOS\WooCommercePOS\Services; |
| 17 | 17 | |
| 18 | -use Ramsey\Uuid\Uuid; | |
| 18 | +use WCPOS\WooCommercePOS\Services\Settings; | |
| 19 | +use WCPOS\WooCommercePOS\Sync\Pos_Uuid; | |
| 19 | 20 | use WP_User; |
| 20 | 21 | use const WCPOS\WooCommercePOS\VERSION as PLUGIN_VERSION; |
| 21 | 22 | |
| 22 | 23 | /** |
| @@ -52,8 +53,30 @@ | ||
| 52 | 53 | */ |
| 53 | 54 | const CAPTURE_PATH = '/capture/'; |
| 54 | 55 | |
| 55 | 56 | /** |
| 57 | + * De-dup window for impressions on AMBIENT upsell placements. | |
| 58 | + * | |
| 59 | + * An ambient placement renders as a side effect of unrelated work — the | |
| 60 | + * product editor, the plugins list — so a merchant re-arms a daily window | |
| 61 | + * simply by doing their job. Live data made the cost obvious: with a daily | |
| 62 | + * window `product_edit_price` alone logged 40,362 impressions from 414 | |
| 63 | + * users (~97 each), and `upgrade_cta_viewed` grew to 90% of every event the | |
| 64 | + * project holds. That does not measure interest, it measures how often | |
| 65 | + * someone edits products, and it makes view -> click conversion meaningless | |
| 66 | + * (0.015% on that placement). | |
| 67 | + * | |
| 68 | + * A month still answers "was this CTA on screen for this merchant", which | |
| 69 | + * is the only question the upgrade funnel asks of an impression. | |
| 70 | + * | |
| 71 | + * Navigational placements — a settings tab, the landing page — keep the | |
| 72 | + * shorter default: the merchant chose to go there, so the visit is signal. | |
| 73 | + * | |
| 74 | + * @var int | |
| 75 | + */ | |
| 76 | + const AMBIENT_IMPRESSION_TTL = MONTH_IN_SECONDS; | |
| 77 | + | |
| 78 | + /** | |
| 56 | 79 | * HTTP request timeout in seconds. |
| 57 | 80 | * |
| 58 | 81 | * Kept low because capture is fire-and-forget. We set |
| 59 | 82 | * `blocking => false` in practice, but the timeout still applies to |
| @@ -105,9 +128,13 @@ | ||
| 105 | 128 | if ( null !== $this->enabled_cache ) { |
| 106 | 129 | return $this->enabled_cache; |
| 107 | 130 | } |
| 108 | 131 | |
| 109 | - $consent = woocommerce_pos_get_settings( 'general', 'tracking_consent' ); | |
| 132 | + // The PERSISTED consent, not the filtered read view — otherwise any | |
| 133 | + // plugin filtering woocommerce_pos_general_settings could switch | |
| 134 | + // telemetry on for a merchant who declined it. Same gate as | |
| 135 | + // Services\Error_Reporter. | |
| 136 | + $consent = Settings::instance()->raw_tracking_consent(); | |
| 110 | 137 | $this->enabled_cache = ( 'allowed' === $consent ); |
| 111 | 138 | |
| 112 | 139 | return $this->enabled_cache; |
| 113 | 140 | } |
| @@ -128,15 +155,26 @@ | ||
| 128 | 155 | * No-op unless analytics is enabled. Automatically attaches the |
| 129 | 156 | * current user's UUID as `distinct_id`, groups the event under the |
| 130 | 157 | * site UUID, and merges in a small set of default context properties. |
| 131 | 158 | * |
| 132 | - * @param string $event Event name, e.g. `pro_link_clicked`. | |
| 133 | - * @param array $properties Event properties. Caller-supplied values | |
| 134 | - * take precedence over defaults. | |
| 159 | + * @param string $event Event name, e.g. `pro_link_clicked`. | |
| 160 | + * @param array $properties Event properties. Caller-supplied values | |
| 161 | + * take precedence over defaults. | |
| 162 | + * @param string $distinct_id_override Identity to attribute the event to. | |
| 163 | + * Defaults to the current user's UUID. | |
| 164 | + * Used by group identification and by | |
| 165 | + * scheduled events, which run without a | |
| 166 | + * logged-in user. | |
| 167 | + * @param string $timestamp ISO-8601 event time. Defaults to now. | |
| 168 | + * Set it when reporting something that | |
| 169 | + * happened earlier — an install event | |
| 170 | + * held back until consent was granted | |
| 171 | + * must keep its real install date or the | |
| 172 | + * retention cohorts are wrong. | |
| 135 | 173 | * |
| 136 | 174 | * @return bool True when a request was dispatched, false otherwise. |
| 137 | 175 | */ |
| 138 | - public function capture( string $event, array $properties = array() ): bool { | |
| 176 | + public function capture( string $event, array $properties = array(), string $distinct_id_override = '', string $timestamp = '' ): bool { | |
| 139 | 177 | if ( ! $this->is_enabled() ) { |
| 140 | 178 | return false; |
| 141 | 179 | } |
| 142 | 180 | |
| @@ -143,9 +181,9 @@ | ||
| 143 | 181 | if ( '' === $event ) { |
| 144 | 182 | return false; |
| 145 | 183 | } |
| 146 | 184 | |
| 147 | - $distinct_id = $this->get_distinct_id(); | |
| 185 | + $distinct_id = '' !== $distinct_id_override ? $distinct_id_override : $this->get_distinct_id(); | |
| 148 | 186 | if ( '' === $distinct_id ) { |
| 149 | 187 | return false; |
| 150 | 188 | } |
| 151 | 189 | |
| @@ -167,9 +205,9 @@ | ||
| 167 | 205 | 'api_key' => $this->get_token(), |
| 168 | 206 | 'event' => $event, |
| 169 | 207 | 'distinct_id' => $distinct_id, |
| 170 | 208 | 'properties' => $merged_properties, |
| 171 | - 'timestamp' => gmdate( 'c' ), | |
| 209 | + 'timestamp' => '' !== $timestamp ? $timestamp : gmdate( 'c' ), | |
| 172 | 210 | ); |
| 173 | 211 | |
| 174 | 212 | return $this->send( self::CAPTURE_PATH, $payload ); |
| 175 | 213 | } |
| @@ -264,8 +302,17 @@ | ||
| 264 | 302 | if ( '' === $group_type || '' === $group_key ) { |
| 265 | 303 | return false; |
| 266 | 304 | } |
| 267 | 305 | |
| 306 | + // A group identification describes the site, not a person. When no user | |
| 307 | + // is logged in — the scheduled property refresh runs from cron — fall | |
| 308 | + // back to PostHog's own convention of keying the event by the group | |
| 309 | + // itself, so the refresh is not silently dropped for want of an identity. | |
| 310 | + $distinct_id = $this->get_distinct_id(); | |
| 311 | + if ( '' === $distinct_id ) { | |
| 312 | + $distinct_id = $group_type . '_' . $group_key; | |
| 313 | + } | |
| 314 | + | |
| 268 | 315 | return $this->capture( |
| 269 | 316 | '$groupidentify', |
| 270 | 317 | array( |
| 271 | 318 | '$group_type' => $group_type, |
| @@ -270,9 +317,10 @@ | ||
| 270 | 317 | array( |
| 271 | 318 | '$group_type' => $group_type, |
| 272 | 319 | '$group_key' => $group_key, |
| 273 | 320 | '$group_set' => $properties, |
| 274 | - ) | |
| 321 | + ), | |
| 322 | + $distinct_id | |
| 275 | 323 | ); |
| 276 | 324 | } |
| 277 | 325 | |
| 278 | 326 | /** |
| @@ -315,13 +363,12 @@ | ||
| 315 | 363 | |
| 316 | 364 | /** |
| 317 | 365 | * Get the distinct ID for the current user. |
| 318 | 366 | * |
| 319 | - * Returns the user's POS UUID meta, lazily provisioning it if | |
| 320 | - * missing. This matches the existing pattern in | |
| 321 | - * `Templates\Frontend` for users who load the POS frontend, and | |
| 322 | - * ensures analytics events from the WP admin (where `Frontend` is | |
| 323 | - * never loaded) still have a stable `distinct_id`. | |
| 367 | + * Delegates to Pos_Uuid — the sole authority for `_woocommerce_pos_uuid` — so | |
| 368 | + * analytics events carry the SAME identity the /cashier and /customers | |
| 369 | + * endpoints serve, lazily provisioning it for admin-only installs (where the | |
| 370 | + * POS frontend has never loaded). | |
| 324 | 371 | * |
| 325 | 372 | * Empty string when no user is logged in. |
| 326 | 373 | */ |
| 327 | 374 | public function get_distinct_id(): string { |
| @@ -329,17 +376,9 @@ | ||
| 329 | 376 | if ( ! $user instanceof WP_User || 0 === $user->ID ) { |
| 330 | 377 | return ''; |
| 331 | 378 | } |
| 332 | 379 | |
| 333 | - $uuid = get_user_meta( $user->ID, '_woocommerce_pos_uuid', true ); | |
| 334 | - if ( \is_string( $uuid ) && '' !== $uuid ) { | |
| 335 | - return $uuid; | |
| 336 | - } | |
| 337 | - | |
| 338 | - $uuid = Uuid::uuid4()->toString(); | |
| 339 | - update_user_meta( $user->ID, '_woocommerce_pos_uuid', $uuid ); | |
| 340 | - | |
| 341 | - return $uuid; | |
| 380 | + return Pos_Uuid::ensure_user_uuid( $user ); | |
| 342 | 381 | } |
| 343 | 382 | |
| 344 | 383 | /** |
| 345 | 384 | * Get the site UUID used as the `site` group key. |
| @@ -348,17 +387,21 @@ | ||
| 348 | 387 | * installs (fresh plugin activation, no POS frontend load yet) |
| 349 | 388 | * still have a stable site identifier for grouping. |
| 350 | 389 | */ |
| 351 | 390 | public function get_site_id(): string { |
| 352 | - $uuid = get_option( 'woocommerce_pos_uuid', '' ); | |
| 353 | - if ( \is_string( $uuid ) && '' !== $uuid ) { | |
| 354 | - return $uuid; | |
| 391 | + // The deactivation hook runs even when Activator::init() bailed on the | |
| 392 | + // WooCommerce check — in that request `new Init()` never ran, so | |
| 393 | + // wcpos-functions.php is not loaded and the helper does not exist. | |
| 394 | + // Read the option directly rather than fataling; an install that has | |
| 395 | + // ever run properly already has one, and a site that has not is not | |
| 396 | + // worth provisioning an identity for on its way out. | |
| 397 | + if ( ! \function_exists( 'wcpos_get_site_uuid' ) ) { | |
| 398 | + $uuid = get_option( 'woocommerce_pos_uuid', '' ); | |
| 399 | + | |
| 400 | + return \is_string( $uuid ) ? $uuid : ''; | |
| 355 | 401 | } |
| 356 | 402 | |
| 357 | - $uuid = Uuid::uuid4()->toString(); | |
| 358 | - update_option( 'woocommerce_pos_uuid', $uuid ); | |
| 359 | - | |
| 360 | - return $uuid; | |
| 403 | + return wcpos_get_site_uuid(); | |
| 361 | 404 | } |
| 362 | 405 | |
| 363 | 406 | /** |
| 364 | 407 | * Whether the given event name is a PostHog-reserved identifier |
| @@ -375,11 +418,27 @@ | ||
| 375 | 418 | */ |
| 376 | 419 | private function get_default_properties(): array { |
| 377 | 420 | return array( |
| 378 | 421 | 'plugin_version' => PLUGIN_VERSION, |
| 379 | - 'pro_active' => class_exists( '\WCPOS\WooCommercePOSPro\WooCommercePOSPro' ), | |
| 422 | + 'pro_active' => $this->is_pro_active(), | |
| 380 | 423 | 'locale' => get_locale(), |
| 381 | 424 | ); |
| 425 | + } | |
| 426 | + | |
| 427 | + /** | |
| 428 | + * Whether the Pro plugin is active, safe to call before Init has run. | |
| 429 | + * | |
| 430 | + * Same situation as get_site_id(): the deactivation hook can fire in a | |
| 431 | + * request where the WooCommerce check failed, Init never ran, and | |
| 432 | + * wcpos-functions.php is not loaded. Fall back to the constant the helper | |
| 433 | + * itself reads rather than fataling on the way out. | |
| 434 | + */ | |
| 435 | + private function is_pro_active(): bool { | |
| 436 | + if ( ! \function_exists( 'wcpos_is_pro_active' ) ) { | |
| 437 | + return \defined( 'WCPOS\WooCommercePOSPro\VERSION' ); | |
| 438 | + } | |
| 439 | + | |
| 440 | + return wcpos_is_pro_active(); | |
| 382 | 441 | } |
| 383 | 442 | |
| 384 | 443 | /** |
| 385 | 444 | * Dispatch a non-blocking HTTPS POST to the PostHog ingestion host. |