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

Troubleshooting Errors

If you hit an error on your WordPress site, follow these short steps to find the cause and fix it.

  1. Reproduce & capture details
    Note the exact time of the error (include your timezone), the URL where it happens, and grab a screenshot.
    If possible, reproduce it twice so you have a reliable timestamp window.

  2. Check server error logs (easiest method)
    Use our partner tool WP Debug Toolkit.
    It aggregates PHP, web server, and database errors in one place and adds handy filters. Get a discount with codeAWPDOC.

  3. Manual logging (if you can’t use the tool)
    Enable WordPress debug logging, then reproduce the error:
    // Add/edit in wp-config.php (above the line “/* That's all, stop editing! */”)
    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true ); // writes to wp-content/debug.log
    define( 'WP_DEBUG_DISPLAY', false ); // hides notices from visitors
    @ini_set( 'display_errors', 0 );

    After reproducing, download /wp-content/debug.log.

  4. Get FTP/SFTP access (if needed)
    Ask your host for SFTP credentials (host, port, username, password or SSH key). Connect, then:
    • Download /wp-content/debug.log.
    • Look for server logs like error_log (Apache/Nginx) or ask your host where they are, or even to provide these logs themselves.

  5. Share the right info with support
    Send us (or your host): the exact error time & timezone, your IP (from whatismyip.com), a screenshot with the URL, and only the relevant log lines around that time window (not the entire log).

  6. Common quick fixes
    • Temporarily disable the last plugin/theme you changed; test again.
    • Clear any caching layer/CDN and your browser cache.
    • Increase resource limits if you see timeouts or memory errors:
      • memory_limit (PHP)
      • max_execution_time (PHP)
      • FcgidIOTimeout (Apache/mod_fcgid) or upstream timeouts (Nginx)

  7. Can’t find errors?
    Ask your host to confirm that PHP, web server, and database error logging are enabled and to provide entries for your timestamp window. If needed, ask to be escalated to a higher-tier technician.
Tip: WP Debug Toolkit streamlines everything above, enabling logging, viewing errors, and exporting only what support needs. You can obtain it with a discount using the code AWPDOCduring checkout.