Overview
Sometimes it is useful to view message performance based on your contacts’ most recent engagement data. To help improve visibility into how your messages are performing with your most engaged to least engaged audience, we have provided custom Smarty code to add to your messages. Once the code is added to your messages, you will be able to see engagement performance for contacts across several levels of engagement.
How Engagement is Defined
Engagement in this example is defined by the last time a contact opened or clicked a message from your account, including contacts who subscribed within the defined lookback time periods. We then categorize contacts into three broad groups. Recently Engaged includes contacts who have opened or clicked an email, or have subscribed in the past 14 days. We also have two more groups for 14-90 and 90-365 engaged, Semi-Recently Engaged and Not Recently Engaged respectively, as well as a group for contacts who have not engaged in over 365 days, which is labeled Never Engaged.
In the example below, you can click on the tabs to view the HTML include code, the message Smarty tag and the filtered stats results.
{*. Place this include in an HTML include and name it engagement_stats. *}
{* Version 1.4 (The Sparkpost Version) *}
{$engagementTrancheStats.debug = 0}
{* sets the current time as $data.now for future use *}
{$data.engagementGroup = 'Not Engaged'}
{$data.contactCreatedDaysAgo = 99999}
{$data.lastEngagementDays = 99999}
{$data.openedDaysInPast = 99999}
{$data.clickedDaysInPast = 99999}
{$data.now = $smarty.now}
{* pulling opens from the last 180 days *}
{$openEvents.eventName = 'open'}
{$openEvents.newerThan = '-180'|date_format:'%Y-%m-%dT%H:%M:%S+0000'}
{$openEvents.properties = []}
{$openEvents.limit = 1}
{$openEvents.sort = ['column'=>'ats','direction'=>'DESC']}
{$openEvents.data = $utils->getEventRecords($openEvents.eventName,$openEvents.newerThan,$openEvents.properties,$openEvents.limit,$openEvents.sort)}
{* pulling clicks from the last 180 days *}
{$clickEvents.eventName = 'click'}
{$clickEvents.newerThan = '-180'|date_format:'%Y-%m-%dT%H:%M:%S+0000'}
{$clickEvents.properties = []}
{$clickEvents.limit = 1}
{$clickEvents.sort = ['column'=>'ats','direction'=>'DESC']}
{$clickEvents.data = $utils->getEventRecords($clickEvents.eventName,$clickEvents.newerThan,$clickEvents.properties,$clickEvents.limit,$clickEvents.sort)}
{* Set engagement as unix timestamps *}
{* if there are clicks it set the most recent click as a variable $data.lastEngagementDays *}
{if $clickEvents.data|count > 0}
{$data.clickedDaysInPast = (($data.now - ($clickEvents.data.0.ats|strtotime)) / 60 / 60 / 24)|round:0}
{$data.lastEngagementDays = $data.clickedDaysInPast}
{/if}
{* if there are opens it determines in the open or click is most recent and then sets that as $data.lastEngagementDays *}
{if $openEvents.data|count > 0}
{$data.openedDaysInPast = (($data.now - ($openEvents.data.0.ats|strtotime)) / 60 / 60 / 24)|round:0}
{if $data.openedDaysInPast < $data.lastEngagementDays}
{$data.lastEngagementDays = $data.openedDaysInPast}
{else}
{$data.lastEngagementDays = $data.openedDaysInPast}
{/if}
{/if}
{* determines if the contact was created in the last 14 days and if so sets a engagementGroup *}
{$data.contactCreatedDaysAgo = (($data.now - ($contact.createdAt|strtotime)) / 60 / 60 / 24)|round:0}
{if $data.contactCreatedDaysAgo <= 14}
{$data.engagementGroup = "New Recipients"}
{else}
{* finding the engagement group by comparing the last engagement date to the engagement criteria *}
{if $data.lastEngagementDays <= 14}
{$data.engagementGroup = 'Recently Engaged'}
{elseif $data.lastEngagementDays <= 90}
{$data.engagementGroup = 'Semi-Recently Engaged'}
{elseif $data.lastEngagementDays <= 180}
{$data.engagementGroup = 'Not Recently Engaged'}
{else}
{$data.engagementGroup = 'Not Engaged'}
{/if}
{/if}
{* adds engagement group as send tag for easy stats filtering *}
{$utils->addSendTag({$data.engagementGroup})}
{* debugging tool to confirm it is working correctly *}
{if $engagementTrancheStats.debug == 1}
<h2>Debug $data</h2>
{$utils->jsonPrettyPrint($data)}
<h2>Debug $clickEvents</h2>
{$utils->jsonPrettyPrint($clickEvents)}
<h2>Debug $openEvents</h2>
{$utils->jsonPrettyPrint($openEvents)}
<h2>Debug $contact</h2>
{$utils->jsonPrettyPrint($contact)}
{/if}
{include 'content:engagement_stats' scope='parent' assign='blackhole'}
<!-- Message HTML -->

HTML Content Include Code
To keep the HTML in your message clutter-free, we will paste the necessary Smarty code into an HTML content include and then insert that content into the message using a short Smarty "include" tag.
Navigate to Content > HTML Content, create a new content piece, title it "engagement_stats" and make sure the key matches the title exactly, then paste the following code:
{*. Place this include in an HTML include and name it engagement_stats. *}
{* Version 1.4 (The Sparkpost Version) *}
{$engagementTrancheStats.debug = 0}
{* sets the current time as $data.now for future use *}
{$data.engagementGroup = 'Not Engaged'}
{$data.contactCreatedDaysAgo = 99999}
{$data.lastEngagementDays = 99999}
{$data.openedDaysInPast = 99999}
{$data.clickedDaysInPast = 99999}
{$data.now = $smarty.now}
{* pulling opens from the last 60 days *}
{$openEvents.eventName = 'open'}
{$openEvents.newerThan = '-60'|date_format:'%Y-%m-%dT%H:%M:%S+0000'}
{$openEvents.properties = []}
{$openEvents.limit = 1}
{$openEvents.sort = ['column'=>'ats','direction'=>'DESC']}
{$openEvents.data = $utils->getEventRecords($openEvents.eventName,$openEvents.newerThan,$openEvents.properties,$openEvents.limit,$openEvents.sort)}
{* pulling clicks from the last 60 days *}
{$clickEvents.eventName = 'click'}
{$clickEvents.newerThan = '-60'|date_format:'%Y-%m-%dT%H:%M:%S+0000'}
{$clickEvents.properties = []}
{$clickEvents.limit = 1}
{$clickEvents.sort = ['column'=>'ats','direction'=>'DESC']}
{$clickEvents.data = $utils->getEventRecords($clickEvents.eventName,$clickEvents.newerThan,$clickEvents.properties,$clickEvents.limit,$clickEvents.sort)}
{* Set engagement as unix timestamps *}
{* if there are clicks it set the most recent click as a variable $data.lastEngagementDays *}
{if $clickEvents.data|count > 0}
{$data.clickedDaysInPast = (($data.now - ($clickEvents.data.0.ats|strtotime)) / 60 / 60 / 24)|round:0}
{$data.lastEngagementDays = $data.clickedDaysInPast}
{/if}
{* if there are opens it determines in the open or click is most recent and then sets that as $data.lastEngagementDays *}
{if $openEvents.data|count > 0}
{$data.openedDaysInPast = (($data.now - ($openEvents.data.0.ats|strtotime)) / 60 / 60 / 24)|round:0}
{if $data.openedDaysInPast < $data.lastEngagementDays}
{$data.lastEngagementDays = $data.openedDaysInPast}
{else}
{$data.lastEngagementDays = $data.openedDaysInPast}
{/if}
{/if}
{* determines if the contact was created in the last 14 days and if so sets a engagementGroup *}
{$data.contactCreatedDaysAgo = (($data.now - ($contact.createdAt|strtotime)) / 60 / 60 / 24)|round:0}
{if $data.contactCreatedDaysAgo <= 14}
{$data.engagementGroup = "New Recipients"}
{else}
{* finding the engagement group by comparing the last engagement date to the engagement criteria *}
{if $data.lastEngagementDays <= 14}
{$data.engagementGroup = 'Recently Engaged'}
{elseif $data.lastEngagementDays <= 90}
{$data.engagementGroup = 'Semi-Recently Engaged'}
{elseif $data.lastEngagementDays <= 180}
{$data.engagementGroup = 'Not Recently Engaged'}
{else}
{$data.engagementGroup = 'Not Engaged'}
{/if}
{/if}
{* adds engagement group as send tag for easy stats filtering *}
{$utils->addSendTag({$data.engagementGroup})}
{* debugging tool to confirm it is working correctly *}
{if $engagementTrancheStats.debug == 1}
<h2>Debug $data</h2>
{$utils->jsonPrettyPrint($data)}
<h2>Debug $clickEvents</h2>
{$utils->jsonPrettyPrint($clickEvents)}
<h2>Debug $openEvents</h2>
{$utils->jsonPrettyPrint($openEvents)}
<h2>Debug $contact</h2>
{$utils->jsonPrettyPrint($contact)}
{/if}
Message Code
After creating your HTML content include, create a new message and paste the following Smarty tag at the top of your message content. This ensures that the HTML include content is rendered at the time message is sent. Your contacts will not see any of the included Smarty code responsible for tracking engagement stats.
{include 'content:engagement_stats' scope='parent' assign='blackhole'}
<!-- Message HTML -->
The same Smarty tag can be used in Sculpt messages as long as it is included within the HTML code of your Sculpt template:

Stats Filters Results
After your message has finished sending, view the results in the Stats Filters section of the message performance page (Performance for batch or Aggregates for automated messages).
The stat filters we added will not be immediately visible. Click the Filter by tag button to add the desired labels to the stats filter table. Only those filters that have data associated with them will be available to select. Over time, the Smarty code will capture and return engagement data based on contacts' interaction with your message
Once added, the tags will remain in the Stats Filters section of your message but can be removed by clicking the X button for each filter to the right of the Stats Filters table (scrolling the Stats Filters table to the right may be required depending on your display settings).

Engagement tags after being added to the Stats Filters page displaying message performance for each engagement group:

Comments
0 comments
Please sign in to leave a comment.