Page Analytics Sparkline

A Lava shortcode that displays page view analytics for the current page, showing total views over a specified time period with percentage change from the previous period and a sparkline chart visualization.

image

I just placed it at the top of the page with our other admin buttons, for now:

image

Features

  • Configurable time period for page view count (default: 30 days)
  • Percentage change comparison with previous period
  • Daily sparkline chart showing view trends
  • Color-coded percentage indicators (green ↑, red ↓, gray →)
  • Parameterized configuration - no need to edit the shortcode

Setup

1. Create the Lava Shortcode

  • Navigate to Admin Tools > CMS Configuration > Lava Shortcuts
  • Add a new shortcode named pageAnalyticsSparkline
  • Paste the provided Lava code into the shortcode template
  • Enable the 'SQL' command

2. Find Your Interaction Channel ID

To find your interaction channel ID:

  • Go to Admin Tools > Reporting > Interactions
  • Locate your external website channel and note the ID
  • You'll use this ID as a parameter when calling the shortcode

3. Add to Your Site / Pages

Place the shortcode in a block that appears on pages where you want analytics displayed. You must specify the interactionchannelid parameter:

{[ pageAnalyticsSparkline interactionchannelid:'10' ]}

To add to all pages, add the block to the Zone that says 'Blocks from Site'.

Parameters

  • interactionchannelid (required): The interaction channel ID for the page analytics

    {[ pageAnalyticsSparkline interactionchannelid:'10' ]}
    
  • days (optional): Number of days for the time period (default: 30)

    {[ pageAnalyticsSparkline interactionchannelid:'10' days:'14' ]}
    

Parameter Examples

//- Default 30-day period
{[ pageAnalyticsSparkline interactionchannelid:'10' ]}

//- Custom 14-day period
{[ pageAnalyticsSparkline interactionchannelid:'10' days:'14' ]}

//- 7-day period for weekly analytics
{[ pageAnalyticsSparkline interactionchannelid:'10' days:'7' ]}

4. Secure the Visibility

To ensure the shortcode is only visible to authorized users, secure the visibility using block settings or wrap it in a conditional statement. Use HasRightsTo to check if the user has edit rights to the page:

{% assign hasPageEditRights = 103 | HasRightsTo:'Edit','Rock.Model.Page' | AsBoolean %}
{% if hasPageEditRights %}
    {[ pageAnalyticsSparkline interactionchannelid:'10' ]}
{% endif %}

Notes

  • Analytics data is based on Rock's built-in interaction tracking
  • Query timeout is set to 10 seconds, but typically completes in ~100µs
  • Displays data for the specific page where the shortcode is placed
  • The shortcode automatically compares the specified period with the previous period of the same length
  • All parameters are passed directly to the shortcode, eliminating the need to edit the shortcode template