'', 'site_id' => '', 'mode' => 'consented_sessions', // aggregate | consented_sessions 'hash_policy' => 0, 'click_goals' => 1, 'consent' => 'propio', // propio | integrado 'woocommerce' => 1, 'exclude_logged_in' => 0, ); } /** * Opciones guardadas, fusionadas con los valores por defecto. */ function ownmetry_options(): array { return wp_parse_args( get_option( 'ownmetry_settings', array() ), ownmetry_defaults() ); } /** * true si el plugin está configurado (base_url y site_id presentes). */ function ownmetry_is_configured(): bool { $opts = ownmetry_options(); return '' !== $opts['base_url'] && '' !== $opts['site_id']; } /** * Registra la opción y su saneado. */ add_action( 'admin_init', 'ownmetry_register_settings' ); function ownmetry_register_settings(): void { register_setting( 'ownmetry', 'ownmetry_settings', array( 'type' => 'array', 'sanitize_callback' => 'ownmetry_sanitize', ) ); } /** * Sanea y valida la entrada del formulario. * * @param mixed $input */ function ownmetry_sanitize( $input ): array { $input = is_array( $input ) ? $input : array(); $out = ownmetry_defaults(); $out['base_url'] = esc_url_raw( trim( (string) ( $input['base_url'] ?? '' ) ) ); $out['site_id'] = sanitize_key( (string) ( $input['site_id'] ?? '' ) ); $out['mode'] = in_array( $input['mode'] ?? '', array( 'aggregate', 'consented_sessions' ), true ) ? (string) $input['mode'] : 'consented_sessions'; $out['hash_policy'] = empty( $input['hash_policy'] ) ? 0 : 1; $out['click_goals'] = empty( $input['click_goals'] ) ? 0 : 1; $out['woocommerce'] = empty( $input['woocommerce'] ) ? 0 : 1; $out['exclude_logged_in'] = empty( $input['exclude_logged_in'] ) ? 0 : 1; $out['consent'] = in_array( $input['consent'] ?? '', array( 'propio', 'exento', 'integrado' ), true ) ? (string) $input['consent'] : 'propio'; return $out; } /** * Página de ajustes. */ add_action( 'admin_menu', 'ownmetry_admin_menu' ); function ownmetry_admin_menu(): void { add_options_page( __( 'Ownmetry', 'ownmetry' ), __( 'Ownmetry', 'ownmetry' ), 'manage_options', 'ownmetry', 'ownmetry_render_settings' ); } /** * Renderiza la página de ajustes. */ function ownmetry_render_settings(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $opts = ownmetry_options(); ?>


$opts['site_id'], 'base' => $base, 'mode' => $is_exento ? 'aggregate' : $opts['mode'], 'hashPolicy' => (bool) $opts['hash_policy'], 'autoSpa' => false, // WordPress hace recargas completas, no navegación SPA. 'clickGoals' => (bool) $opts['click_goals'], 'consentBanner' => array( 'enabled' => ! $is_integrado, 'mode' => $is_exento ? 'exempt' : 'consent', 'showCredit' => false, // "Powered by Ownmetry" opt-in: desactivado por defecto. ), ); $handle = 'ownmetry-tracker'; wp_enqueue_script( $handle, $base . 'tracker.php', array(), null, false ); wp_add_inline_script( $handle, 'window.ownmetryConfig = ' . wp_json_encode( $config ) . ';window.localyticsConfig = window.ownmetryConfig;', 'before' ); if ( ownmetry_wc_enabled() ) { // Listener de "añadido al carrito" (evento JS de WooCommerce). wp_add_inline_script( $handle, "document.addEventListener('added_to_cart',function(){if(window.ownmetry){window.ownmetry.track('add_to_cart');}});", 'after' ); } } /** * El tracker es un módulo ES: necesita type="module". */ add_filter( 'script_loader_tag', 'ownmetry_script_tag', 10, 2 ); function ownmetry_script_tag( string $tag, string $handle ): string { if ( 'ownmetry-tracker' === $handle ) { $tag = str_replace( ' src=', ' type="module" src=', $tag ); } return $tag; } /** * WooCommerce: eventos de compra (add_to_cart y purchase). */ /** true si WooCommerce está activo y el seguimiento de compras está habilitado. */ function ownmetry_wc_enabled(): bool { if ( ! ownmetry_is_configured() ) { return false; } $opts = ownmetry_options(); return ! empty( $opts['woocommerce'] ) && class_exists( 'WooCommerce' ); } /** * Compra completada, en la página de agradecimiento. Sin identificadores ni PII: * solo el importe total del pedido. */ add_action( 'woocommerce_thankyou', 'ownmetry_wc_purchase' ); function ownmetry_wc_purchase( $order_id ): void { if ( ! ownmetry_wc_enabled() ) { return; } $order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : null; if ( ! $order ) { return; } $total = (float) $order->get_total(); $handle = 'ownmetry-wc-purchase'; wp_register_script( $handle, '', array(), null, true ); wp_enqueue_script( $handle ); wp_add_inline_script( $handle, 'document.addEventListener("DOMContentLoaded",function(){if(window.ownmetry){window.ownmetry.track("purchase",{total:' . wp_json_encode( $total ) . '});}});', 'after' ); }