2

I've built some AMPscript code that selects two different contentblocks to be displayed next to each other in the same block. The code retrieves all the blocks that have the content_type "themablok".

The first block is selected by randomly selecting an index between 1 and the total rowcount divided by two.

The second block is selected by randomly selecting an index between the total rowcount divided by two and the total rowcount. This makes sure that the code doesn't select the same row twice.

It works great, except when I try to view the email in my browser via the %%view_email_url%% string. The AMPscript seems to get rendered again, and I get totally different blocks in the HTML mail. How can I make sure that the blocks I received in the mail clients don't differ from the blocks I see when I press the 'View this email as HTML' button?

Code:

/* initiate variables and lookup blocks */
   var @blocks, @block, @blockCount
   set @blocks = LookupRows("contentBlokkenTest", "content_type", "themablok", "thema", @FavouriteTheme)
   set @blockCount = rowcount(@blocks)
   if @blockCount > 0 then

   /* Define variables */
   var @subidBlock

   /* Set variables block 1*/
   SET @themeIndexHalf = Divide(@blockCount, 2)
   SET @themeIndexHalf = FormatNumber(@themeIndexHalf, "N0")

   set @randomThemeIndex = Random(1, Subtract(@themeIndexHalf, 1))

   set @block = row(@blocks,@randomThemeIndex) /* get random theme block #1 */
   set @subidBlock1 = field(@block,"subid")

   set @subidBlockset = "true"


   /* Block 2, get second half of indexes */ 
   set @randomThemeIndex = Random(@themeIndexHalf, @blockCount)

   set @block = row(@blocks,@randomThemeIndex) /* get random theme block #2 */
   set @subidBlock2 = field(@block,"subid")

   if @subidBlock2 == @historyBlockSubid AND @blockCount > 1 then
   set @randomThemeIndex = Random(@themeIndexHalf, @blockCount)
   set @block = row(@blocks,@randomThemeIndex) /* get next random theme block #2 */
   set @subidBlock2 = field(@block,"subid")

   else
   set @block = row(@blocks,@randomThemeIndex) /* get theme block #1 */
   set @subidBlock2 = field(@block,"subid")

   endif


   set @subidBlockset = "true"

   /* fallback content subid */
   else
   set @subidBlock1 = "lxtr"
   set @subidBlock2 = "lmgr1"

   endif
   /* END: getting thema content blocks */
1

AMPscript is processed server side and you are correct, the VAWP will process it again as it is hitting the server again.

To get around this, you will need to:

  1. Create a Data Extension to hold your send time info
  2. Create a VAWP conditional statement
  3. Add an upsert to DE for live send
  4. Add a lookup to DE for VAWP

Create a DE that holds JobID (pkey) | SubscriberKey (pkey) | Block

JobID - To identify the send
SubscriberKey - to identify the subscriber inside of the send
Block - to identify assigned block at send time

Create a message context conditional statement:

%%[

   if AttributeValue("_messagecontext") != "VAWP" THEN
   /* Sets normal send time activity */   

      /* Put your normal AMPscript code here to assign block */

      if AttributeValue("_messagecontext") == "SEND" THEN
      /* Upsert to DE for live send */

        UpsertDE('yourDE',2,'JobID',AttributeValue("jobid"),'SubscriberKey', AttributeValue("_subscriberkey"), 'Block',@yourBlockValue)

      endif

  else
  /* VAWP behavior */

      SET @yourBlockVar = Lookup('yourDE','Block','JobID',AttributeValue("jobid"),'SubscriberKey', AttributeValue("_subscriberkey"))

  endif

]%%
  • Thanks for the answer! I will try this tomorrow and report back with the results. – Rutger Nov 27 '18 at 16:24
  • Is there a specific reason why you use the messagecontext with the Attributevalue function? We evaluate it directly in our scripts and it works for us. Just wondering... – Stephan de Paly Nov 27 '18 at 19:17
  • I use the AttributeValue function for all personalization strings or attributes as a form of defensive coding - It has a better null handling - increasing ability for error handling and reducing unnecessary 'fatal errors' – Gortonington Nov 27 '18 at 20:45
  • I tried this method and it works great, thanks a lot! The only things I had to change was to put VAWP between quotation marks and change the UpdateDE to UpsertDE. – Rutger Nov 28 '18 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.