# Separated Hook and Context System - Implementation Guide

## Overview

Successfully separated hook information from context in the logging system. Hooks are now stored in a dedicated database column for precise filtering, while context focuses on general functional steps.

## Database Schema Changes

### New Table Structure:

```sql
CREATE TABLE wp_tracknow_debug_logs (
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    time datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
    request_id varchar(32) DEFAULT NULL,
    session_id varchar(64) DEFAULT NULL,
    context varchar(100) DEFAULT NULL,        -- General functional step
    hook varchar(100) DEFAULT NULL,           -- Specific WordPress/WC hook
    order_id mediumint(9) DEFAULT NULL,
    message text NOT NULL,
    PRIMARY KEY (id),
    KEY time_index (time),
    KEY request_index (request_id),
    KEY session_index (session_id),
    KEY context_index (context),
    KEY hook_index (hook),                    -- New index for hook filtering
    KEY order_index (order_id)
);
```

### Migration Support:

- ✅ Automatic migration adds `hook` column to existing tables
- ✅ Preserves all existing data
- ✅ Adds proper indexing for fast filtering
- ✅ Backward compatible with existing logs

## Simplified Context Categories

### General Functional Steps:

- **`click_tracking`** - Click ID tracking and management
- **`click_retrieve`** - Retrieving stored click IDs
- **`click_save`** - Saving click IDs (cookies, user meta, checkout)
- **`click_generation`** - Auto-generating click IDs
- **`coupon_retrieve`** - Retrieving coupon information
- **`order_processing`** - Order status processing
- **`order_complete`** - Order completion
- **`order_cancel`** - Order cancellation
- **`order_refund`** - Order refunds
- **`payment_complete`** - Payment completion
- **`payment_processing`** - Payment processing
- **`pixel_build`** - Building tracking pixel URLs
- **`pixel_send`** - Sending tracking pixels
- **`pixel_render`** - Rendering frontend pixels
- **`api_update`** - API communication
- **`asset_load`** - Loading CSS/JS assets

## Hook Information Storage

### Captured Hooks (stored in `hook` column):

```
// Enqueue hooks
wp_tracknowfwc_enqueue_scripts
admin_enqueue_scripts

// WooCommerce checkout/order hooks
before_woocommerce_pay_form
woocommerce_checkout_update_order_meta
woocommerce_checkout_create_order
woocommerce_store_api_checkout_order_processed
woocommerce_thankyou

// WooCommerce order status hooks
woocommerce_order_status_processing
woocommerce_pre_payment_complete
woocommerce_payment_complete
woocommerce_order_status_completed
woocommerce_order_status_cancelled
woocommerce_order_status_refunded

// WordPress core hooks
init
wp_footer
shutdown
```

## Enhanced Filtering Capabilities

### 1. Filter by Context (General Steps):

```sql
-- All click-related operations
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE 'click_%';

-- All payment operations
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE 'payment_%';

-- All order operations
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE 'order_%';

-- All pixel operations
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE 'pixel_%';

-- Coupon retrieval
SELECT * FROM wp_tracknow_debug_logs
WHERE context = 'coupon_retrieve';
```

### 2. Filter by Hook (Specific Triggers):

```sql
-- All payment completion hooks
SELECT * FROM wp_tracknow_debug_logs
WHERE hook = 'woocommerce_payment_complete';

-- All init hook activity
SELECT * FROM wp_tracknow_debug_logs
WHERE hook = 'init';

-- All checkout-related hooks
SELECT * FROM wp_tracknow_debug_logs
WHERE hook LIKE 'woocommerce_checkout_%';

-- All order status hooks
SELECT * FROM wp_tracknow_debug_logs
WHERE hook LIKE 'woocommerce_order_status_%';
```

### 3. Combined Context + Hook Filtering:

```sql
-- Click retrieval during init
SELECT * FROM wp_tracknow_debug_logs
WHERE context = 'click_retrieve'
AND hook = 'init';

-- Pixel sending during payment completion
SELECT * FROM wp_tracknow_debug_logs
WHERE context = 'pixel_send'
AND hook = 'woocommerce_payment_complete';

-- Order processing during status changes
SELECT * FROM wp_tracknow_debug_logs
WHERE context = 'order_processing'
AND hook LIKE 'woocommerce_order_status_%';
```

### 4. Advanced Filtering Examples:

```sql
-- All click operations during checkout process
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE 'click_%'
AND hook LIKE 'woocommerce_checkout_%'
ORDER BY time;

-- Payment-related operations for specific order
SELECT * FROM wp_tracknow_debug_logs
WHERE context LIKE 'payment_%'
AND order_id = 12345
ORDER BY time;

-- Hook activity during business hours
SELECT hook, context, COUNT(*) as frequency
FROM wp_tracknow_debug_logs
WHERE TIME(time) BETWEEN '09:00:00' AND '17:00:00'
GROUP BY hook, context
ORDER BY frequency DESC;
```

## Admin Interface Enhancements

### Context Filter Dropdown:

```
- Click Operations (click_%)
- Order Operations (order_%)
- Payment Operations (payment_%)
- Pixel Operations (pixel_%)
- Coupon Operations (coupon_%)
- API Operations (api_%)
- Asset Loading (asset_load)
```

### Hook Filter Dropdown:

```
- Init Hook (init)
- Footer Hook (wp_footer)
- Payment Complete (woocommerce_payment_complete)
- Order Processing (woocommerce_order_status_processing)
- Checkout Create (woocommerce_checkout_create_order)
- Thank You Page (woocommerce_thankyou)
```

### Combined Filters:

- **Context**: `click_retrieve` + **Hook**: `init` = Click retrieval during page initialization
- **Context**: `order_processing` + **Hook**: `woocommerce_order_status_processing` = Order processing operations

## Example Log Entries

### Before (Combined Context):

```
Context: pixel_send|hook_payment_complete|woocommerce_payment_complete
Message: Processing order 12345
```

### After (Separated Structure):

```
Context: pixel_send
Hook: woocommerce_payment_complete
Message: Processing order 12345
```

## CSV Export Enhancements

### Export by Context:

- **Filter**: `context = 'click_retrieve'` → All click retrieval operations
- **Filter**: `context LIKE 'order_%'` → All order-related operations

### Export by Hook:

- **Filter**: `hook = 'woocommerce_payment_complete'` → All payment completion events
- **Filter**: `hook LIKE 'woocommerce_checkout_%'` → All checkout-related hooks

### Export by Context + Hook:

- **Filters**: `context = 'pixel_send' AND hook = 'woocommerce_thankyou'` → Thank you page pixel sending

## Performance Benefits

### Database Optimization:

- **Separate Indexes**: Fast filtering on both context and hook columns
- **Normalized Data**: Cleaner data structure reduces storage overhead
- **Query Efficiency**: Precise filtering without LIKE operations on combined strings

### Filtering Speed:

- **Context Queries**: `WHERE context = 'click_retrieve'` (exact match)
- **Hook Queries**: `WHERE hook = 'init'` (exact match)
- **Combined Queries**: Uses both indexes simultaneously

## Migration and Backward Compatibility

### Automatic Migration:

```sql
-- Automatically adds hook column
ALTER TABLE wp_tracknow_debug_logs ADD COLUMN hook varchar(100) DEFAULT NULL;
ALTER TABLE wp_tracknow_debug_logs ADD KEY hook_index (hook);
```

### Data Preservation:

- ✅ All existing logs remain intact
- ✅ New logs use separated structure
- ✅ Old admin interface continues to work
- ✅ CSV exports include both context and hook columns

## Real-World Use Cases

### 1. E-commerce Flow Analysis:

```sql
-- Complete checkout to payment flow
SELECT context, hook, message, time
FROM wp_tracknow_debug_logs
WHERE order_id = 12345
AND (context LIKE 'checkout_%' OR context LIKE 'payment_%')
ORDER BY time;
```

### 2. Performance Monitoring:

```sql
-- Hook execution frequency
SELECT hook, COUNT(*) as executions
FROM wp_tracknow_debug_logs
WHERE time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY hook
ORDER BY executions DESC;
```

### 3. Error Debugging:

```sql
-- Find errors in specific functional area
SELECT * FROM wp_tracknow_debug_logs
WHERE context = 'click_retrieve'
AND message LIKE '%error%'
ORDER BY time DESC;
```

The separated hook and context system provides **surgical precision** for debugging while maintaining clean, normalized data structure that's optimized for both filtering performance and user experience! 🎯
