Resolving macros in Dynamic Newsletter

   —   
Subscriber macros like for example {%FirstName%} are not properly resolved in content of your pages when sent as a dynamic newsletter. This behavior is by design in Kentico up to version 6.0 (included) which makes it impossible to personalize the dynamic newsletter per subscriber. This article describes how to workaround this implementation.
Whenever the dynamic newsletter is sent-out, the same content is used for all of the subscribers which makes it impossible to personalize. In order to send personalized e-mail of some dynamic page, features of both static and dynamic newsletters needs to be combined. The idea here involves using custom macro, which will in content of static newsletter load content of the dynamic page on which personalized macros will be used and resolved later.

First of all, let’s create the dynamic newsletter and disable it by un-checking the “Schedule mails-out” setting.

screen1.png

Secondly, we need to create static newsletter and some issue containing our custom macro which ideally should hold code name of our dynamic newsletter ( {#dynamic#} ). In this case, we need to make sure that the header, footer and body of the main newsletter template do not contain any code except for one editable region.

screen2.png

screen3.png

Next, let’s customize the dynamic page and include replacement macro(s), which will be later replaced by our custom code. The reason why we need to come up with custom replacement macros in this case is that if we would use standard context macros like {%FirstName%}, they will be resolved by the system in time when the subscriber information is not yet available, thus we will end up with empty areas in location(s) where the context macros were used. Instead of using context macros, we need to use our custom replacement macros like {§FirstName§}.

screen4.png

Finally, the last step involves development of custom macro, which will load the dynamic page and replace our custom replacement macros. Here is a code example:

     /// <summary>
     /// Resolves the custom macro
     /// </summary>
     /// <param name="sender">Sender</param>
     /// <param name="e">Event arguments</param>
     private void MacroResolver_OnResolveCustomMacro(object sender, MacroEventArgs e)
     {
         if (!e.Match)
         {
             // Add your custom macro evaluation
             switch (e.Expression.ToLower())
             {
                 default:
                       // Get dynamic newsletter
                       InfoDataSet<Newsletter> newsletter = NewsletterProvider.GetNewsletters("NewsletterName = '" + e.Expression + "'", "", 1, "NewsletterID");
 
                     if (newsletter.Items != null && newsletter.Items.Count > 0)
                     {
                         e.Match = true;
                         e.Result = e.Expression;
                        
                         try
                         {
                             int newsletterId = ValidationHelper.GetInteger(DataHelper.GetDataRowValue(newsletter.Tables[0].Rows[0], "NewsletterID"), 0);
                             int issueId = EmailQueueManager.GenerateDynamicIssue(newsletterId);
                             if (issueId > 0)
                             {
                                 Issue issue = IssueProvider.GetIssue(issueId);
                                 if (issue != null)
                                 {
                                     e.Result = issue.IssueText.Replace("{§", "{ %").Replace("§}", " %}");
                                 }
                             }
                         }
                         catch { }
                     }
                     break;
             }
         }
     }
 Note for the code: Please remove the spaces in these expressions:  "{ %" and " %}". 

The result should look like following, personalized per each subscriber:

screen5.png

This way, we can also imlement personalized unsubscription link: {§UnsubscribeLink§}

One disadvantage of this implementation is  that we can only schedule newsletter sending once and then manually do it later each week, month etc. (depends on our needs). This is  feature of static newsletter which is used as a base in our implementation. However, we can create custom scheduled task which will ensure sending of our static newsletter once per configured time.

Note: Personalization of dynamic newsletter is possible in the out-of-box solution from version 7.0.

Here is an examle on how to personalize your dynamic newsletter in version 7.x while using macros:

{%FirstName|(resolver)subscriber%}
{%LastName|(resolver)subscriber%}
{%UnsubscribeLink|(resolver)subscriber%}
{%NewsletterGUID|(resolver)newsletterissue%}

As you can see, you need to explicitly say from which object (used resolver) the system should load/resolve the data.

-mr-


Applies to: Kentico CMS 6.x
Share this article on   LinkedIn