AMPscript Best Practices Part 3: Planning for maintainability

This is the third and final post in the AMPscript best practices series. If you missed the second post, you can access it  here.

In order to make your AMPscript code easier to maintain, there are three key practices to follow:
  1. Modular design
  2. Documentation
  3. Logging Exceptions/Errors

Modular design

Consider if there will be any use cases where you can reuse the same code. If so, write the code in a separate block and refer to it in your email. This promotes code reusability and reduces redundancy.

Documentation

Documentation plays a crucial role in understanding code in the long run. In AMPscript, you can enhance the readability of comments by using HTML tags. This can transform the look of your comments in code previews, making them more informative and visually appealing. Creating a template for documentation can save time, especially for emails with heavy AMPscript usage.

Ampscript Code without HTML Tags:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
%%[ /* This code is to display if booking is done for a subscriber
Author: Sravan Alaparthi
Created: 8/14/2020
Last modified: 8/14/2020
Last modification comments: initial creation
*/ 

/*Declare variables*/
var @Booking

/*Set variable values*/
Set @Booking='Completed'

]%%

%%[
/*Check if booking is completed*/
IF NOT Empty(@Booking) THEN ]%%
Your booking for the event is  %%=v(@Booking)=%%
%%[ ENDIF ]%%

Code editor preview:


Ampscript Code with HTML Tags:

Let's see how the code looks when documented with HTML tags as below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
%%[ /* <h3 style="color:red">This code is to display if booking is done for a subscriber</h3>
<b style="color:blue">Author:</b> Sravan Alaparthi</br>
<b style="color:blue">Created:</b> 8/14/2020</br>
<b style="color:blue">Last modified:</b> 8/14/2020</br>
<b style="color:blue">Last modification comments:</b> initial creation</br></br></br>
*/ 

/*<b style="color:Orange">Declare variables</b></br>*/
var @Booking

/*<b style="color:Orange"></br></br>Set variable values </br></b>*/
Set @Booking='Completed'

]%%

%%[
/*<b style="color:Orange"></br></br>Check if booking is completed</br></b>*/
IF NOT Empty(@Booking) THEN ]%%
Your booking for the event is  %%=v(@Booking)=%%
%%[ ENDIF ]%%

Code editor preview:



Logging Exceptions/Errors

Debugging AMPscript can be challenging, especially when dealing with failures in large-scale sends. By implementing an exception logging data extension, you can capture essential send attributes and AMPscript variables when errors occur. This enables you to identify the subscriber causing the error and investigate the issue more effectively.

An example of AMPscript exception logging with Server-Side JavaScript (SSJS) is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<script runat="server">
Platform.Load("Core","1.1.1");
try{
</script>

%%[ your AMPscript block goes here  ]%%

<script runat="server">
}catch(e){

//Get required Ampscript ariable
//Variable.GetValue("@myAmpVariable");


//write them to DE with time stamp as created date
InsertDE('SomeDE','Error',Stringify(e), 'subscriberkey',subscriberkey, 'CreatedDate',NOW())
 Write();
}
</script>

This is the third and final post on AMPscript best practices, hope you had a good read!!

No comments:

Post a Comment

Powered by Blogger.