Skip to main content

Enabling Tracking Stats

Highlight and Share can be used to track stats for the social networks you interact with.

Overview​

When a user shares content (via a social button or Web Share), the plugin can send a share event to your analytics stack. Stats are on by default; you can turn them off globally or restrict which data is sent for privacy.

Three channels receive the same event (when stats are enabled):

ChannelPurpose
dataLayerGoogle Tag Manager (and any GTM-based tags).
gtagGoogle Analytics 4 when gtag() is present on the page.
CustomEventCustom listeners (e.g. window.addEventListener('has:share', ...)).

If stats are disabled, nothing is pushed to any channel.


Event name​

ContextEvent name
dataLayer / CustomEventhas:share
GA4 (gtag)has:share (same; GA4 params are separate).

Data sent (payload)​

The event carries a small payload. By default, URL, share text, and title are not sent (they are sent as empty strings). Enable enhanced mode to include them.

dataLayer & CustomEvent (camelCase)​

FieldWhen populatedDescription
eventAlwaysEvent name, e.g. has:share.
hasShareTypeAlwaysType of share, e.g. text, image.
hasSocialNetworkAlwaysNetwork or method, e.g. twitter, pinterest, webshare.
hasShareTextEnhanced onlySelected or shared text (privacy-sensitive).
hasSharePostUrlEnhanced onlyURL being shared (privacy-sensitive).
hasSharePostTitleEnhanced onlyTitle of the content (privacy-sensitive).

GA4 (gtag) — snake_case parameters​

ParameterWhen populatedDescription
has_share_typeAlwaysSame as hasShareType.
has_social_networkAlwaysSame as hasSocialNetwork.
has_share_textEnhanced onlySame as hasShareText.
has_share_post_urlEnhanced onlySame as hasSharePostUrl.
has_share_post_titleEnhanced onlySame as hasSharePostTitle.

Synthetic events (CustomEvent)​

The plugin dispatches a synthetic CustomEvent on window so your own scripts (or GTM custom HTML tags) can react to shares without relying on dataLayer or gtag.

What is sent​

The event is dispatched as:

  • Event type: has:share
  • Target: window
  • Options: { bubbles: true, cancelable: false }
  • Payload: The same payload as dataLayer is in event.detail (camelCase). When enhanced is off, hasShareText, hasSharePostUrl, and hasSharePostTitle are empty strings.
PropertyTypeDescription
event.detail.eventstringEvent name (has:share).
event.detail.hasShareTypestringShare type, e.g. text, image.
event.detail.hasSocialNetworkstringNetwork/method, e.g. twitter, webshare.
event.detail.hasShareTextstringShare text (empty unless enhanced).
event.detail.hasSharePostUrlstringShared URL (empty unless enhanced).
event.detail.hasSharePostTitlestringContent title (empty unless enhanced).

How to listen​

Attach a listener to window for the event type has:share. Read the payload from event.detail.

window.addEventListener( 'has:share', function( event ) {
var payload = event.detail;
console.log( 'Share:', payload.hasSocialNetwork, payload.hasShareType );
// Optional: send to your own analytics or API.
} );
// With enhanced enabled, you can use URL, text, and title.
window.addEventListener( 'has:share', function( event ) {
var payload = event.detail;
if ( payload.hasSharePostUrl ) {
console.log( 'Shared URL:', payload.hasSharePostUrl );
}
} );

The listener runs only when stats are enabled (has_stats_enabled / HAS_STATS_ENABLED). If stats are disabled, no CustomEvent is dispatched.


Constants​

Define these in wp-config.php (or before the plugin runs) to set defaults. Filters can still override them.

HAS_STATS_ENABLED​

  • Default: Stats are enabled (no constant = tracking on).
  • Use: Set to false to turn off all stats (dataLayer, gtag, and CustomEvent).
// In wp-config.php: disable all share tracking.
define( 'HAS_STATS_ENABLED', false );

HAS_STATS_ENHANCED​

  • Default: Enhanced data is off (no constant = URL, text, and title are not sent).
  • Use: Set to true to allow sending URL, share text, and title in the payload.
// In wp-config.php: allow sending URL, share text, and title.
define( 'HAS_STATS_ENHANCED', true );

Filters​

Use these in your theme or plugin to override the constants or implement conditional logic.

has_stats_enabled​

  • Purpose: Turn stats on or off for the frontend.
  • Default passed: Value from HAS_STATS_ENABLED if defined, otherwise true.
  • Return: true to enable, false to disable.
// Disable all stats (e.g. for EU unless consent given).
add_filter( 'has_stats_enabled', function( $enabled ) {
return false; // or your consent check.
} );

// Or use the helper.
add_filter( 'has_stats_enabled', '__return_false' );
// Enable only when a consent cookie is set.
add_filter( 'has_stats_enabled', function( $enabled ) {
return isset( $_COOKIE['analytics_consent'] ) && $_COOKIE['analytics_consent'] === 'yes';
} );

has_stats_enhanced​

  • Purpose: Allow or disallow sending URL, share text, and title (privacy-sensitive fields).
  • Default passed: Value from HAS_STATS_ENHANCED if defined, otherwise false.
  • Return: true to send enhanced fields, false to send empty strings for them.
// Enable enhanced data (URL, text, title) when stats are on.
add_filter( 'has_stats_enhanced', '__return_true' );
// Enable enhanced only for logged-in admins (example).
add_filter( 'has_stats_enhanced', function( $enhanced ) {
return current_user_can( 'manage_options' );
} );