# Hook-Based Filtering System - Complete Guide

## Overview

Enhanced the TracknowWC logging system to capture and store all specific WooCommerce hooks you're listening to, making it easy to filter logs by hook type in your database.

## Captured Hooks

All of the following hooks are now automatically detected and stored in the log context:

### Enqueue Hooks

- `wp_tracknowfwc_enqueue_scripts` → Context: `hook_enqueue`
- `admin_enqueue_scripts` → Context: `hook_admin_enqueue`
- `wp_enqueue_scripts` → Context: `hook_public_enqueue`

### WooCommerce Checkout/Order Hooks

- `before_woocommerce_pay_form` → Context: `hook_before_pay`
- `woocommerce_checkout_update_order_meta` → Context: `hook_checkout_meta`
- `woocommerce_checkout_create_order` → Context: `hook_checkout_create`
- `woocommerce_store_api_checkout_order_processed` → Context: `hook_api_checkout`
- `woocommerce_thankyou` → Context: `hook_thankyou`

### WooCommerce Order Status Hooks

- `woocommerce_order_status_processing` → Context: `hook_status_processing`
- `woocommerce_pre_payment_complete` → Context: `hook_pre_payment`
- `woocommerce_payment_complete` → Context: `hook_payment_complete`
- `woocommerce_order_status_completed` → Context: `hook_status_completed`
- `woocommerce_order_status_cancelled` → Context: `hook_status_cancelled`
- `woocommerce_order_status_refunded` → Context: `hook_status_refunded`

### WordPress Core Hooks Used by TracknowWC

- `init` → Context: `hook_init`
- `wp_footer` → Context: `hook_footer`
- `shutdown` → Context: `hook_shutdown`

## Enhanced Context Format

Each log entry now includes rich context information in this format:

```
{method_context}|{hook_context}|{actual_hook_name}
```

### Example Log Contexts:

```
pixel_send|hook_payment_complete|woocommerce_payment_complete
cookie_save|hook_init|init
checkout_save|hook_checkout_create|woocommerce_checkout_create_order
pixel_render|hook_thankyou|woocommerce_thankyou
wc_hook:order_status_processing|hook_status_processing|woocommerce_order_status_processing
```

## Database Filtering Examples

### 1. Filter by Hook Type (Easy Categories)

```sql
-- All payment-related hooks
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_payment%';

-- All checkout-related hooks
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_checkout%';

-- All order status changes
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_status%';

-- All enqueue-related logs
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_enqueue%';
```

### 2. Filter by Specific Hook

```sql
-- Only woocommerce_payment_complete
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%woocommerce_payment_complete%';

-- Only init hook
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%|init';

-- Only thank you page
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%woocommerce_thankyou%';
```

### 3. Filter by Action Type + Hook

```sql
-- Pixel sending during payment completion
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE 'pixel_send|hook_payment_complete%';

-- Cookie saving during init
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE 'cookie_save|hook_init%';

-- Order processing during status change
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_status_processing%';
```

### 4. Combined Filters with Order IDs

```sql
-- Payment completion for specific order
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_payment_complete%'
AND order_id = 12345;

-- All checkout hooks for orders in date range
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_checkout%'
AND created_at >= '2025-09-01'
AND created_at <= '2025-09-30';
```

## Admin Interface Filtering

With the enhanced logging, you can now filter in the admin interface by:

### Quick Filters:

- **Payment Hooks**: `hook_payment_complete`, `hook_pre_payment`
- **Checkout Hooks**: `hook_checkout_create`, `hook_checkout_meta`, `hook_api_checkout`
- **Status Hooks**: `hook_status_processing`, `hook_status_completed`, `hook_status_cancelled`
- **Page Hooks**: `hook_thankyou`, `hook_init`, `hook_footer`

### Advanced Filters:

- **Exact Hook**: Enter full hook name like `woocommerce_payment_complete`
- **Hook Category**: Use patterns like `hook_payment*` or `hook_checkout*`
- **Method + Hook**: Combine like `pixel_send|hook_payment*`

## Real-World Filtering Scenarios

### 1. Debugging Payment Issues:

```sql
-- All payment-related activity for an order
SELECT * FROM wp_tracknow_debug_logs
WHERE order_id = 12345
AND (context LIKE '%hook_payment%' OR context LIKE '%hook_pre_payment%')
ORDER BY created_at;
```

### 2. Tracking Checkout Flow:

```sql
-- Complete checkout process for a session
SELECT * FROM wp_tracknow_debug_logs
WHERE session_id = 'sess_abc123'
AND context LIKE '%hook_checkout%'
ORDER BY created_at;
```

### 3. Monitoring Order Status Changes:

```sql
-- All status changes in the last 24 hours
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_status%'
AND created_at >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
ORDER BY created_at DESC;
```

### 4. Performance Analysis:

```sql
-- Enqueue performance during high traffic
SELECT COUNT(*) as log_count,
       SUBSTRING_INDEX(context, '|', 1) as action_type
FROM wp_tracknow_debug_logs
WHERE context LIKE '%hook_enqueue%'
AND created_at >= DATE_SUB(NOW(), INTERVAL 1 HOUR)
GROUP BY action_type;
```

## CSV Export Filtering

When exporting logs as CSV, you can now:

1. **Export by Hook Type**:

   - Filter: `hook_payment_complete`
   - Result: All payment completion logs

2. **Export Checkout Flow**:

   - Filter: `hook_checkout*`
   - Result: Complete checkout process logs

3. **Export Order Processing**:
   - Filter: `hook_status*`
   - Result: All order status change logs

## Integration with Existing Features

This hook filtering works seamlessly with all existing logging features:

- ✅ **UTC Timestamps**: All hook-filtered logs include precise timing
- ✅ **Pagination**: Filter results are paginated efficiently
- ✅ **Order ID Tracking**: Combine hook filters with order ID filters
- ✅ **Session Tracking**: Track hooks across user sessions
- ✅ **CSV Export**: Export filtered hook logs to CSV
- ✅ **10MB Database Limit**: Automatic cleanup preserves hook filtering

## Benefits Summary

### For Developers:

- **Precise Debugging**: Know exactly which hook triggered each log
- **Flow Tracking**: Follow complete order/checkout flows
- **Performance Monitoring**: Identify slow hooks
- **Issue Resolution**: Quickly isolate hook-specific problems

### For Site Administrators:

- **Easy Filtering**: Simple dropdown filters for common hook types
- **Targeted Exports**: Export only relevant hook data
- **Clean Reports**: Filter out noise to focus on specific processes
- **Performance Insights**: Monitor hook execution patterns

### For Support Teams:

- **Faster Troubleshooting**: Direct hook-based problem identification
- **Comprehensive Context**: Full hook execution context in each log
- **Customer-Specific**: Filter by order ID + hook for customer issues
- **Historical Analysis**: Track hook behavior over time

The enhanced logging system now provides **surgical precision** for debugging WooCommerce integration issues, making it easy to trace exactly which hooks are firing when and why! 🎯
