| 1 |
<?php |
| 2 |
/** |
| 3 |
* PostHog Client for sending events. |
| 4 |
* |
| 5 |
* @package WpVr\Tracking |
| 6 |
* @since 8.5.37 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Wpvr\Admin\Tracking; |
| 10 |
use Exception; |
| 11 |
use PostHog\PostHog; |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Class PosthogClient |
| 20 |
* |
| 21 |
* Responsible for sending event data to PostHog via HTTP POST. |
| 22 |
* |
| 23 |
* @package WpVr\Tracking |
| 24 |
* @since 8.5.37 |
| 25 |
*/ |
| 26 |
class PosthogClient { |
| 27 |
|
| 28 |
/** |
| 29 |
* PostHog Project API Key. |
| 30 |
* |
| 31 |
* @since 8.5.37 |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private const POSTHOG_API_KEY = 'phc_2RJD4rdX4y49xQYOXDAKCaw1Sm62sJxKonr9y8cVK7f'; |
| 35 |
|
| 36 |
/** |
| 37 |
* PostHog project ID. |
| 38 |
* |
| 39 |
* @since 8.5.37 |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
private $project_id = '80684'; |
| 43 |
|
| 44 |
/** |
| 45 |
* PostHog API endpoint for capturing events. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
private const POSTHOG_HOST = 'https://eu.posthog.com'; |
| 51 |
|
| 52 |
/** |
| 53 |
* Captures an event and sends it to PostHog. |
| 54 |
* Only captures events on WooCommerce Feed related pages. |
| 55 |
* |
| 56 |
* @since 8.5.37 |
| 57 |
* |
| 58 |
* @param string $event_name The name of the event. |
| 59 |
* @param array $properties An array of properties for the event. |
| 60 |
* @param string|null $distinct_id The distinct ID for the user. If null, uses admin email. |
| 61 |
* |
| 62 |
* @return bool True if the event was sent successfully, false otherwise. |
| 63 |
*/ |
| 64 |
public function capture( $event_name, $properties = [], $distinct_id = 0 ) { |
| 65 |
if ( empty( self::POSTHOG_API_KEY ) ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
if ( ! $distinct_id ) { |
| 70 |
$distinct_id = $this->get_distinct_id(); |
| 71 |
} |
| 72 |
|
| 73 |
// Add session context and plugin specific properties |
| 74 |
$properties = array_merge( $properties, [ |
| 75 |
'page_url' => isset($_SERVER['REQUEST_URI']) ? esc_url( $_SERVER['REQUEST_URI'] ) : '', |
| 76 |
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ) : '', |
| 77 |
'plugin' => 'WP VR', |
| 78 |
'plugin_version' => defined('WPVR_VERSION') ? WPVR_VERSION : 'unknown', |
| 79 |
'wp_version' => get_bloginfo( 'version' ), |
| 80 |
'site_url' => get_site_url(), |
| 81 |
]); |
| 82 |
|
| 83 |
try { |
| 84 |
PostHog::init( |
| 85 |
self::POSTHOG_API_KEY, |
| 86 |
[ |
| 87 |
'host' => self::POSTHOG_HOST, |
| 88 |
] |
| 89 |
); |
| 90 |
$response = \PostHog\PostHog::capture([ |
| 91 |
'distinctId' => $distinct_id, |
| 92 |
'event' => $event_name, |
| 93 |
'properties' => $properties, |
| 94 |
]); |
| 95 |
return $response; |
| 96 |
} catch (Exception $e) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get distinct ID for PostHog tracking. |
| 103 |
* |
| 104 |
* @since 8.5.37 |
| 105 |
* |
| 106 |
* @return string Distinct ID |
| 107 |
*/ |
| 108 |
private function get_distinct_id(): string { |
| 109 |
if ( is_user_logged_in() ) { |
| 110 |
$current_user = wp_get_current_user(); |
| 111 |
return $current_user->user_email; |
| 112 |
} |
| 113 |
return 'anonymous_' . uniqid( '', true ); // Generate a unique ID for anonymous users |
| 114 |
} |
| 115 |
|
| 116 |
} |