Skip to content

[Simple Analytics] Add Data Layer & Form Event Tracking #198

@iamdharmesh

Description

@iamdharmesh

Description

Create a custom database table for daily aggregated form analytics, implement client-side form view tracking via JavaScript, and hook into the existing submission flow to record submissions.

Technical Context:

  • No custom tables exist today, all storage is wp_options.
  • Upgrade routines live in mailchimp_upgrade.php using plugins_loaded.
  • Form views require client-side JS tracking (AJAX call when form loads in browser)
  • Frontend JS is enqueued in mailchimp_sf_load_resources() as mailchimp_sf_main_js
  • Form submissions are handled by Mailchimp_Form_Submission::handle_form_submission() in includes/class-mailchimp-form-submission.php
  • Block forms embed mailchimp_sf_list_id in hidden inputs, shortcode/widget forms use mc_list_id option

Implementation Breakdown

1. Custom Database Table

  • Create includes/class-mailchimp-analytics-data.php with Mailchimp_Analytics_Data class.
  • Table: {prefix}_mailchimp_sf_form_analytics with columns (High level overview, detailed columns to be determined during implementation): id, list_id, form_id (will be used in the future to track multiple forms), event_date, views, submissions; UNIQUE KEY on (list_id, form_id, event_date).
  • Implement create_table() via dbDelta(), increment_views(), increment_submissions() using INSERT ... ON DUPLICATE KEY UPDATE.
  • Implement get_analytics_data( $list_id, $start_date, $end_date ) and get_totals( $list_id, $start_date, $end_date ) query methods.
  • Add creation to plugin activation hook and migration to mailchimp_upgrade.php; store mailchimp_sf_analytics_db_version option.

2. Client-Side Form View Tracking

  • Register REST/AJAX endpoint to track form views (public access, validates list_id).
  • Add data-list-id attribute to form wrappers or hidden input for list_id in form markup.
  • Add tracking JS in assets/js/mailchimp.js: find .mc_signup_form[data-list-id] on DOM ready, fire async fetch() POST, deduplicate per page load
  • Pass REST/AJAX URL and nonce via wp_localize_script() in mailchimp_sf_load_resources()

3. Form Submission Tracking

  • Add do_action( 'mailchimp_sf_form_submission_success', $list_id ) in Mailchimp_Form_Submission::handle_form_submission() after successful subscription
  • Hook into this action from Mailchimp_Analytics_Data to call increment_submissions()
  • Ensure it fires for both block and shortcode/widget submissions, do NOT track failed/spam submissions

Acceptance Criteria

  • Custom table is created on activation and via upgrade routine for existing installs
  • All queries use $wpdb->prepare()
  • Form views are recorded via JS when forms load in browser.
  • Multiple forms on one page each track their respective list_id
  • Successful submissions increment the correct list's daily count
  • Failed/spam submissions are not tracked

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions