Skip to content

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

Open
alaca wants to merge 14 commits intofeature/analytics-admin-pagefrom
feature/data-layer-and-form-event-tracking
Open

[Simple Analytics] Add Data Layer & Form Event Tracking#204
alaca wants to merge 14 commits intofeature/analytics-admin-pagefrom
feature/data-layer-and-form-event-tracking

Conversation

@alaca
Copy link
Copy Markdown
Collaborator

@alaca alaca commented Mar 24, 2026

Description of the Change

  • Created a custom database table ({prefix}_mailchimp_sf_form_analytics) for daily aggregated form analytics, with columns for list_id, form_id, event_date, views, and submissions (unique key on list_id, form_id, event_date)
  • Implement client-side form view tracking that adds data-list-id attribute to all form types (block, widget, shortcode) and
    fires an AJAX POST on page load, deduplicated per list_id
  • Hook into successful form submissions via a new mailchimp_sf_form_submission_success action to increment daily submission
  • Table creation runs on plugin activation and via the upgrade routine for existing installs

Closes #198

How to test the Change

  • Activate the plugin on a fresh install and verify the wp_mailchimp_sf_form_analytics table is created
  • Visit a page with a Mailchimp signup form and verify a view is recorded in the analytics table for the correct list_id and
    today's date
  • Place multiple forms (different lists) on one page and verify each list gets its own view count
  • Submit a form successfully and verify the submission count increments
  • Submit a form with invalid data (e.g., missing email) and verify no submission is tracked
  • Navigate to Mailchimp > Analytics in wp-admin and verify that test data is displayed

Changelog Entry

Created a custom database table (mailchimp_sf_form_analytics) for daily aggregated form analytics
Implemented client-side form view tracking via AJAX

Credits

Props @alaca

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@github-actions github-actions bot added this to the 2.1.0 milestone Mar 24, 2026
@github-actions github-actions bot added the needs:code-review This requires code review. label Mar 24, 2026
@alaca alaca requested a review from iamdharmesh March 24, 2026 15:51
@iamdharmesh iamdharmesh changed the base branch from develop to feature/analytics-admin-page March 28, 2026 03:45
@iamdharmesh iamdharmesh requested a review from Copilot March 28, 2026 03:45
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new “Simple Analytics” data layer to track Mailchimp signup form views/submissions and surfaces the data in the WP Admin Analytics page.

Changes:

  • Adds a custom DB table ({prefix}_mailchimp_sf_form_analytics) plus query/increment helpers and an AJAX handler for form-view tracking.
  • Emits a new mailchimp_sf_form_submission_success action on successful submissions and hooks it to increment submission counts.
  • Adds data-list-id to rendered forms and adds frontend JS to POST form-view events.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
mailchimp_widget.php Adds data-list-id attribute to widget/shortcode form markup for JS tracking.
includes/blocks/mailchimp/markup.php Adds data-list-id attribute to block form markup for JS tracking.
assets/js/mailchimp.js Adds client-side view tracking that POSTs to admin-ajax.php.
includes/class-mailchimp-form-submission.php Fires a new action on successful form submission for analytics hooks.
includes/class-mailchimp-analytics-data.php New analytics storage/query class, table creation, AJAX handler, and submission tracking hook.
mailchimp.php Loads the analytics data class, registers activation hook, localizes AJAX URL + nonce.
mailchimp_upgrade.php Runs table creation in upgrade routine based on a stored DB version option.
includes/class-mailchimp-analytics.php Removes trailing whitespace only.
includes/admin/templates/analytics.php Fetches analytics data and currently outputs it via print_r() for display.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +28 to +32
public function init() {
add_action( 'wp_ajax_mailchimp_sf_track_form_view', array( $this, 'handle_form_view' ) );
add_action( 'wp_ajax_nopriv_mailchimp_sf_track_form_view', array( $this, 'handle_form_view' ) );
add_action( 'mailchimp_sf_form_submission_success', array( $this, 'track_submission' ) );
}
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR introduces new analytics behaviors (AJAX endpoint for view tracking, DB writes, and a new submission-success hook) but doesn’t add/extend automated coverage. There is an existing Cypress suite under tests/cypress; adding at least one e2e test that verifies view tracking + submission tracking increments the expected counts would help prevent regressions.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

@iamdharmesh iamdharmesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @alaca. Overall this looks good and added few feedback/comment. Could you please check?

alaca and others added 12 commits April 6, 2026 08:20
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Dharmesh Patel <dspatel44@gmail.com>
Co-authored-by: Dharmesh Patel <dspatel44@gmail.com>
@alaca
Copy link
Copy Markdown
Collaborator Author

alaca commented Apr 6, 2026

@iamdharmesh resolved. Also, the data is now loaded via ajax for future integrations with chart.js

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Comments suppressed due to low confidence (1)

includes/admin/templates/analytics.php:110

  • There are two elements with the same id mailchimp-sf-analytics-content (one empty and one containing the placeholder). Duplicate IDs produce invalid markup and getElementById() will return only the first element, leaving the placeholder orphaned/unreachable. Keep a single container and place the placeholder inside it (or remove the duplicate).
			<div class="mailchimp-sf-analytics-content" id="mailchimp-sf-analytics-content">
			</div>

				<div class="mailchimp-sf-analytics-content" id="mailchimp-sf-analytics-content">
					<div class="mailchimp-sf-analytics-placeholder">
						<p><?php esc_html_e( 'Select a date range and list to view analytics.', 'mailchimp' ); ?></p>
					</div>
				</div>

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

const { data } = response;
contentArea.innerHTML = JSON.stringify(data);
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contentArea.innerHTML = JSON.stringify(data) uses innerHTML with server-provided content, which is unnecessary and can introduce XSS risks if the payload ever contains user-controlled strings. Prefer assigning to textContent for raw JSON output, or render into DOM nodes with proper escaping.

Suggested change
contentArea.innerHTML = JSON.stringify(data);
contentArea.textContent = JSON.stringify(data);

Copilot uses AI. Check for mistakes.
);
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared

if ( false === $result ) {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error_log() call will likely violate the WordPress PHPCS sniff WordPress.PHP.DevelopmentFunctions.error_log_error_log (other files use // phpcs:ignore when logging). Either add the appropriate PHPCS ignore/comment, or route logging through the existing logging mechanism/filter used elsewhere in the plugin.

Suggested change
if ( false === $result ) {
if ( false === $result ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Logs DB write failures for analytics tracking.

Copilot uses AI. Check for mistakes.
);
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared

if ( false === $result ) {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error_log() call will likely violate the WordPress PHPCS sniff WordPress.PHP.DevelopmentFunctions.error_log_error_log (other files use // phpcs:ignore when logging). Either add the appropriate PHPCS ignore/comment, or route logging through the existing logging mechanism/filter used elsewhere in the plugin.

Suggested change
if ( false === $result ) {
if ( false === $result ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Intentional diagnostic logging for database write failures.

Copilot uses AI. Check for mistakes.
Comment on lines +28 to +33
public function init() {
add_action( 'wp_ajax_mailchimp_sf_track_form_view', array( $this, 'handle_form_view' ) );
add_action( 'wp_ajax_nopriv_mailchimp_sf_track_form_view', array( $this, 'handle_form_view' ) );
add_action( 'wp_ajax_mailchimp_sf_get_analytics', array( $this, 'handle_get_analytics' ) );
add_action( 'mailchimp_sf_form_submission_success', array( $this, 'track_submission' ) );
}
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New analytics behavior (AJAX endpoints + view/submission tracking) isn’t covered by existing Cypress tests. Since the repo already has e2e coverage for the Analytics admin page, add tests that (1) trigger a form view POST and assert the analytics table/counts change, and (2) submit a form successfully and assert submissions increment (and invalid submissions do not).

Copilot uses AI. Check for mistakes.
Comment on lines +29 to +33
// Create analytics table if it doesn't exist.
$analytics_db_version = get_option( 'mailchimp_sf_analytics_db_version' );
if ( false === $analytics_db_version || version_compare( Mailchimp_Analytics_Data::DB_VERSION, $analytics_db_version, '>' ) ) {
Mailchimp_Analytics_Data::create_table();
}
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This table-creation block won’t run if mailchimp_version_check() returns early when MCSF_VER === get_option('mc_version'). That means a site can end up with no analytics table if the mc_version option is already current (e.g., version string unchanged, or option manually set) but mailchimp_sf_analytics_db_version is missing/outdated. Consider moving the analytics DB version check above the early return, or adjusting the early return condition to also account for the analytics DB version option.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

@iamdharmesh iamdharmesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes @alaca. This looks good overall now. Just added 1 minor nit comment and this will be ready for the go. Thanks

@alaca alaca requested a review from iamdharmesh April 7, 2026 09:55
Copy link
Copy Markdown
Collaborator

@iamdharmesh iamdharmesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for changes @alaca

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs:code-review This requires code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Simple Analytics] Add Data Layer & Form Event Tracking

3 participants