<?php

namespace PageVICO\Tracking;

defined( 'ABSPATH' ) || exit;
class TrackerInit {
    public static function init() {
        // Only enqueue tracking script if conditions are met
        if ( TrackingConditions::shouldTrack() ) {
            add_action( 'wp_enqueue_scripts', [__CLASS__, 'injectScript'] );
        }
    }

    public static function injectScript() {
        if ( is_admin() ) {
            return;
        }
        wp_enqueue_script(
            'pagevico-tracker',
            PageVICO_PLUGIN_URL . 'assets/js/tracker.js',
            [],
            PageVICO_VERSION,
            true
        );
        wp_localize_script( 'pagevico-tracker', 'pagevicoTracker', [
            'ajax_url' => rest_url( 'pagevico/v1/track' ),
            'nonce'    => wp_create_nonce( 'wp_rest' ),
        ] );
    }

}
