Loading
Personalização do Marketing Cloud
Exemplos de código para dados de campanha acionados em emails de engajamento do Marketing Cloud

Exemplos de código para dados de campanha acionados em emails de engajamento do Marketing Cloud

A Personalização do Marketing Cloud envia recomendações e outros dados do catálogo relacionados ao acionador, como itens do carrinho abandonados, para o Marketing Cloud Engagement no formato JSON. Você pode renderizar os dados em seus emails de engajamento usando a Idioma do modelo guiado e o JavaScript no lado do servidor. Estes exemplos de código mostram como renderizar dados de campanha acionados em um bloco de recomendações de email.

Você pode modificar os blocos de código para alterar o número de itens renderizados, atualizar fontes, alterar propriedades da tabela e adicionar HTML para estilo. Para formatação mais avançada, trabalhe com um desenvolvedor.

Dados JSON enviados pelo modelo padrão

[
   {
       "price": 40,
       "imageUrl": "https://www.northerntrailoutfitters.com/on/demandware.static/-/Sites-nto-apparel/default/dw111991b2/images/large/2031004BI9-0.jpg",
       "name": "Women's Classic Mini Shorts",
       "id": "2031004",
       "url": "https://www.northerntrailoutfitters.com/default/women/bottoms/shorts/women%E2%80%99s-classic-mini-shorts-2031004BI9.html"
   },
   {
       "price": 200,
       "imageUrl": "https://www.northerntrailoutfitters.com/on/demandware.static/-/Sites-nto-apparel/default/dw10dbfcd0/images/large/1010414AP0-0.jpg",
       "name": "Men's Rainier L4 Windproof Soft Shell Hoodie",
       "id": "1010414",
       "url": "https://www.northerntrailoutfitters.com/default/men/jackets-%26-vests/windbreaker/men%27s-rainier-l4-windproof-soft-shell-hoodie-1010414AP0.html"
   }
]

Bloco de código de idioma do modelo guiado

%%[
  /**
      Get JSON data from the Marketing Cloud event Data Extension
      and store it in the @Catalog_Data Ampscript variable.
      You should change the field name passed to the AttributeValue
      function depending on the trigger type, use-case and your template
      configuration. Refer to the "Triggered Campaign Templates" documentation
      for more information about the standard field names.
  */
  SET @Catalog_Data = AttributeValue("Recommendations")
]%%
 
<div style="max-height: 100%; max-width: 600px; position: relative; overflow: hidden; margin: 20px auto; text-align: center;">
  <div style="display: table; width: 100%; max-height: 100%;">
      {{.datasource JSONVar type=variable maxRows=8}}
      {{.data}}
          { "target" : "@Catalog_Data" }
      {{/data}}
 
      <span style="display: inline-block; margin-top: 10px;">
          <!-- %%[ SET @href = TreatAsContent("{{url}}") ]%% -->
          <a href="%%=RedirectTo(@href)=%%" style="display: block; width: 300px; overflow: hidden;">
              <img src="{{imageUrl}}" style="border: 0; width: 90%; margin: 10px;">
          </a>
          <div style="padding: 0px; margin: 0px; width: 300px; overflow: hidden;">
              <h3 style="padding: 0px; margin: 10px; font-size: 16px; line-height: 20px; height: 40px; overflow: hidden;">
                  {{name}}
              </h3>
              <p style="padding: 0px; margin: 10px; font-size: 14px;">
                  Price: {{=FormatCurrency(price, "en-US")}}
              </p>
          </div>
      </span>
        
      {{/datasource}}
  </div>
</div>

Bloco de código JavaScript no lado do servidor

<script runat="server">
   Platform.Load("core", "1.1");
 
   var html = '<div style="max-height: 100%; max-width: 600px; position: relative; overflow: hidden; margin: 20px auto; text-align: center;">';
   html += '<div style="display: table; width: 100%; max-height: 100%;">';
 
   try{
       /**
           Get JSON data from the Marketing Cloud event Data Extension
           and store it in the Catalog_Data variable.
           You should change the field name passed to the Attribute.GetValue
           function depending on the trigger type, use-case and your template
           configuration. Refer to the "Triggered Campaign Templates" documentation
           for more information about the standard field names.
       */
       var Catalog_Data = Platform.Function.ParseJSON(Attribute.GetValue('Recommendations'));
 
       for(var i = 0; i < Catalog_Data.length; i++){
           html += '<span style="display: inline-block; margin-top: 10px;">';
           html += '    <a href="' + Catalog_Data[i].url + '" style="display: block; width: 300px; overflow: hidden;">';
           html += '        <img src="' + Catalog_Data[i].imageUrl + '" style="border: 0; width: 90%; margin: 10px;">';
           html += '    </a>';
           html += '    <div style="padding: 0px; margin: 0px; width: 300px; overflow: hidden;">';
           html += '        <h3 style="padding: 0px; margin: 10px; font-size: 16px; line-height: 20px; height: 40px; overflow: hidden;">';
           html += '            ' + Catalog_Data[i].name;
           html += '        </h3>';
           html += '        <p style="padding: 0px; margin: 10px; font-size: 14px;">';
           html += '            Price: ' + Catalog_Data[i].price;
           html += '        </p>';
           html += '    </div>';
           html += '</span>';
       }
   }
   catch(err){
       html += '<!--smth went wrong when rendering items-->';
   }
 
   html += '</div>';
   html += '</div>';
  
   Write(html);
</script>
 
Carregando
Salesforce Help | Article