all things Sitecore related

Use Engagement Analytics reports in code

By on November 18, 2014 in C#, DMS, Engagement Plan, Reports, Sitecore with 0 Comments

In a recent project I was faced with the following requirement:

  • The latest visits report from a specific visitor should be mailed as part of a custom engagement plan action

Creating a custom email action with some tokenized values I implemented very quickly because of this blogpost by Frank van Rooijen:
http://www.newguid.net/sitecore/2013/sending-tokenized-emails-from-a-sitecore-engagement-plan/

But I had to add something to my custom action and that was generating the visitor report from the specific visitor and attach it to the mail I had to send. With the help of reflector and Sitecore support I quickly came to the conclusion that the Sitecore API is my friend. Before showing the code I first need to know what exact report I wanted to attach and which parameter(s) this report needs. In my case I want the Visitor Overview, so I will get the Item ID out of Sitecore:

visitor_overview

visitor_overview

Now I need to know which exact parameters the report needs. I can investigate this by opening the stimulsoft mrt report file under “sitecore\shell\Applications\Analytics\Reports” or I can look in engagement analytics and inspect the querystring. If I open the report in engagement analytics I see the following:

visitor_overview_report

visitor_overview_report

Via the querystring I can see easily that the report expects the parameter “visitorid”, this is important because I have to add this information when I generate the report in my code. With this information I can add the following prototype code to my custom engagement action execute method:



public override AutomationActionResult Execute(VisitorDataSet.AutomationStatesRow automationStatesRow, Item action, bool isBackgroundThread)
{
var lFrom = parameters[SendEmailMessageAction.Parameters.FromEmail];
var lSubject = parameters[SendEmailMessageAction.Parameters.Subject];
var lMail = parameters[SendEmailMessageAction.Parameters.Content], automationStatesRow);
var lTo = parameters[SendEmailMessageAction.Parameters.FixedEmail];
Database lDatabase = Factory.GetDatabase("master");
ID lReportId = new ID("{397506EC-31D1-4C32-A522-C9718389EF5B}");
Item lReportItem = lDatabase.GetItem(lReportId);
StiReport report = ReportFactory.GetReport(lDatabase, lReportId, DateTime.Now,   DateTime.Now.AddYears(-1), "", null);
var lReportParameters = new Dictionary<string, object>
{
{"visitorid", automationStatesRow.VisitorId.ToString()}
};

ReportFactory.ApplyParametersToCompiledReport(report.CompiledReport, lReportParameters);
string str5 = ReportExporter.Export(report, StiExportFormat.Pdf, lReportItem["Report Title"], new object[0]);
MailMessage lMessage = new MailMessage(lFrom, lTo, lSubject, lMail);
lMessage.Attachments.Add(new Attachment(str5));
lMessage.IsBodyHtml = true;
var lSMTPClient = new SmtpClient();
lSMTPClient.Host = “you email host”;
lSMTPClient.Send(lMessage);
}


So to summarize:

  • I use the Sitecore.Analytics.Reports.ReportFactory.GetReport method to get a stimulsoft report object
  • Then I use Sitecore.Analytics.Reports.ReportFactory.ApplyParametersToCompiledReport method to fill the visitorid parameter
  • Then I use Sitecore.Analytics.Reports.ReportExporter.Export to save my generated report to the file system as pdf
  • Finally I can use the generated pdf in my mail object

So that’s it. If you have any questions or remarks on this please let me know.

Ronald.

About the Author

About the Author: I am working as a Team leader at IQuality Business Solutions B.V., specialized in solution development, with a strong focus on Sitecore. Although most of my time busy with developing, I am also making designs, give sitecore training and advise customers. I am Sitecore MVP since 2011. You can follow me on twitter: @rhlnieuwenhuis please also visit our company sitecore subsite on: getsmarterwithsitecore .

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Post a Comment

Your email address will not be published. Required fields are marked *

Top