Developer documentation

Developer documentation

AnalyticsWP is a WordPress-based plugin that provides comprehensive analytics tracking capabilities for your WordPress site. This documentation covers both client-side and server-side tracking implementations.

API Reference

Client-Side Tracking API

Basic Usage - JavaScript

// Track a simple event
AnalyticsWP.event('page_view');

// Track an event with properties
AnalyticsWP.event('button_click', {
unique_event_identifier: 'btn_click_123',
button_id: 'submit-form',
button_text: 'Submit'
});

Full Usage - JavaScript

interface EventProperties {
// Pass a unique identifier if you want to reference this Event later:
unique_event_identifier?: string;
// Pass conversion properties in if it's a conversion event:
conversion_type?: string;
conversion_id?: string;
// All of these below properties are *automatically* added by AnalyticsWP,
// you can still pass them in to override the values if you want:
unique_session_id?: string;
user_id?: number|string;
user_email?: string;
device_type?: string;
ip_address?: string;
utm_source?: string;
utm_medium?: string;
utm_campaign?: string;
utm_term?: string;
utm_content?: string;
timestamp?: string;
// And then literally any custom properties you want to add:
[key: string]: any;
}

function event(event_type: string, event_properties?: EventProperties): void

Server-Side Tracking API

/**
* Track an event from the server side
*
* @param string $event_type
* @param array $args
*
* @return array{error: non-empty-string}|int - An error message or the Event ID.
*/

public static function track_server_event(
string $event_type,
array $args = array(),
)

Basic Usage

Special Event Properties

The following properties have special meaning in AnalyticsWP.
unique_event_identifier
(string)
  • Used for event deduplication
  • Should be unique per event instance
  • Example: order_123, form_submission_456
conversion_type
(string)
  • Identifies the type of conversion
  • Used for categorizing conversion events
  • Example: woocommerce_order, form_submission
conversion_type
(string)
  • Unique identifier for the conversion
  • Often corresponds to an ID in your system
  • Example: Order ID, form_submission ID
user_id
(number|string)
  • Important: AnalyticsWP will automatically use the logged in user's ID, if one is not provided
  • User's email address
  • Used for user identification
  • Example: user@example.com
Automatically detected
unique_session_id
(string)
  • Important: Automatically generated and managed by AnalyticsWP
  • Unique identifier for user's browsing session
  • Persists for 365 days
  • Example: 550e8400-e29b-41d4-a716-446655440000
Automatically detected
referrer
(string)
  • Important: Automatically captured on client-side
  • The URL that referred the user to the current page
  • Used for tracking traffic sources
  • Example: https://google.com
Automatically detected
page_url
(string)
  • Important: Automatically captured on client-side
  • The URL of the page where the event occurred
  • Used for page-level analytics
  • Example: https://example.com/products
Automatically detected
device_type
(string)
  • Important: Automatically captured on client-side
  • The type of device used by the visitor
  • One of: desktop, tablet, or mobile
  • Used for device-specific analytics
Automatically detected
user_agent
(string)
  • Important: Automatically captured on client-side
  • The browser's user agent string
  • Used for browser and OS detection
  • Example: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Automatically detected
user_agent
(string)
  • Important: Automatically captured on client-side
  • The browser's user agent string
  • One of: desktop, tablet, or mobile
  • Used for device-specific analytics
Automatically detected
ip_address
(string)
  • Important: Automatically captured on client-side
  • The visitor's IP address
  • Used for geolocation and security
  • Example: 192.168.1.1
Automatically detected
utm_source
(string)
  • Important: Automatically captured from URL parameters
  • Identifies the source of traffic
  • Part of UTM tracking
  • Example: google, newsletter, facebook
Automatically detected
utm_medium
(string)
  • Important: Automatically captured from URL parameters
  • Identifies the marketing medium
  • Part of UTM tracking
  • Example: cpc, email, social
Automatically detected
utm_campaign
(string)
  • Important: Automatically captured from URL parameters
  • Identifies the specific campaign
  • Part of UTM tracking
  • Example: spring_sale, product_launch
Automatically detected
utm_campaign
(string)
  • Important: Automatically captured from URL parameters
  • Identifies paid search keywords
  • Part of UTM tracking
  • Example: sprunning+shoes, blue+widgets
Automatically detected
utm_content
(string)
  • Important: Automatically captured from URL parameters
  • Used to differentiate similar content or links
  • Part of UTM tracking
  • Example: navlink, banner, sidebar
Automatically detected
timestamp
(string)
  • Important: Automatically set when event is tracked
  • The date and time when the event occurred
  • Stored in MySQL DATETIME format
  • Example: 2024-12-10 10:30:45
Automatically detected

Integration Examples

WooCommerce Integration

This code demonstrates how AnalyticsWP tracks WooCommerce conversion events.
/**
* Track WooCommerce orders as conversions
*
* @param int $order_id
* @return array{error: string}|int|null
*/

function track_order($order_id) {
$order = \wc_get_order($order_id);

$user_id = $order->get_user_id();
$email = $order->get_billing_email();

return Event::track_server_event('conversion', [
'conversion_id' => $order_id,
'conversion_type' => 'woocommerce_order',
'user_id' => $user_id,
'user_email' => $email,
]);
}
add_action('woocommerce_new_order', 'track_order, 10);

Breakdance Form Integration

This below code from Breakdance demonstrates several key integration patterns for AnalyticsWP:
  • Using track_server_event() to track important user actions
  • Using unique_event_identifier to mark an event for future retrieval
  • Using admin_journey_path_for_event_where_condition() to link back to user journeys which included a specific event, in this case: a Breakdance form submission
  • Integration with WordPress hooks for automatic tracking
  • Adding AnalyticsWP data to existing admin screens
These patterns can be easily applied to create countless integration with AnalyticsWP.
namespace Breakdance\Partners\AnalyticsWP;

// Initialize the integration when WordPress plugins are loaded
add_action('plugins_loaded', '\Breakdance\Partners\AnalyticsWP\init_integration');

/**
* Main initialization function for the AnalyticsWP integration with Breakdance forms
* Sets up all the necessary WordPress hooks if AnalyticsWP is installed
*/

function init_integration() {
if (is_analyticswp_installed()) {
// Track form submissions when they're saved to the database
add_action('wp_insert_post', '\Breakdance\Partners\AnalyticsWP\breakdance_form_submission_handler', 10, 3);

// Add a custom column to the form submissions admin screen
add_filter(
'manage_breakdance_form_res_posts_columns',
'\Breakdance\Partners\AnalyticsWP\breakdance_form_analyticswp_custom_column'
);

// Populate the custom column with a link to view the user's journey
add_action('manage_breakdance_form_res_posts_custom_column',
'\Breakdance\Partners\AnalyticsWP\breakdance_form_analyticswp_custom_column_content',
10,
2
);
}
}

/**
* Handles form submissions by tracking them in AnalyticsWP
* This is called automatically by WordPress when a form submission is saved
*
* Key AnalyticsWP Integration Point:
* Uses track_server_event() to track form submissions with a unique identifier
*/

function breakdance_form_submission_handler($post_id, $post, $update) {
if ($post->post_type == 'breakdance_form_res') {
// Track the form submission in AnalyticsWP
\AnalyticsWP\Lib\Event::track_server_event(
'breakdance_form_submission', // Event type
[
// Use a unique identifier to prevent duplicate tracking
'unique_event_identifier' => get_unique_event_identifier_for_form_submission($post_id),
]
);
}
}

/**
* Adds an "AnalyticsWP Journey" column to the form submissions list
*/

function breakdance_form_analyticswp_custom_column($columns) {
$columns['analyticswp_journey'] = 'AnalyticsWP Journey';
return $columns;
}

/**
* Displays a link to view the user's journey for each form submission
*
* Key AnalyticsWP Integration Point:
* Uses admin_journey_path_for_event_where_condition() to generate a URL
* that will show the user's journey leading up to this form submission
*/

function breakdance_form_analyticswp_custom_column_content($column, $post_id) {
if ($column !== 'analyticswp_journey') return;

// Get the journey URL from AnalyticsWP using the unique identifier
$journey_path = \AnalyticsWP\Lib\URLs::admin_journey_path_for_event_where_condition(
['unique_event_identifier' => get_unique_event_identifier_for_form_submission($post_id)]
);

if ($journey_path) {
echo "View Journey";
} else {
echo "No Journey";
}
}

/**
* Creates a unique identifier for a form submission
* This helps prevent duplicate tracking and allows linking back to the journey
*/

function get_unique_event_identifier_for_form_submission($post_id) {
return 'breakdance_form_submission' . '_' . $post_id;
}

/**
* Checks if AnalyticsWP is installed and available
*/

function is_analyticswp_installed() {
return class_exists('\AnalyticsWP\Lib\Core');
}

Security and Validation

AnalyticsWP implements several security measures:
  • Bot detection and filtering
  • Data sanitization and validation
  • XSS prevention through esc_url_raw() and sanitize_text_field()
  • JSON encoding validation
  • Type checking for all parameters
  • Automatic null handling for empty values

Best Practices

  • Always provide event types as strings
  • Use unique event identifiers for critical events like form submissions and orders
  • Include conversion tracking for important business events
  • Implement proper error handling for server-side tracking
  • Type checking for all parameters
  • Use type hints and proper parameter validation