AMPscript 401
このシナリオには、AMPscript の使用に関する次の情報が含まれます。
- メール送信の一部としてのループの使用
- AMPscript を使用したメール送信の外観の変更
- メッセージコンテキストの絞り込み
- コードブロックからのコンテンツの作成
次の AMPscript は、メールメッセージ全体のコンテンツを提供します。
1%%[/* 401 */
2
3Var @memid, @fname, @lname, @prefname, @address, @city, @state, @zip, @mempref, @plat, @rows,
4 @row, @ship
5
6Set @memid = MemberID
7Set @fname = FirstName
8Set @lname = LastName
9Set @prefname = PrefName
10Set @address = Address
11Set @zip = Zip
12Set @mempref = MemPref
13Set @plat = Plat
14
15/* 401 */ ]%%%%[ if not empty(@prefname) then ]%%%%= v(@prefname) =%%%%[ else ]%%%%= v(@fname) =%%%%[ endif ]%%, below are your account detailsName
16First Name:%%= v(@fname) =%%Last Name:%%= v(@lname) =%%
17
18%%[ if not Empty(@prefname) then ]%%Preferred Name:%%= v(@prefname) =%%
19
20%%[ endif ]%%Address
21
22Address:%%= v(@address) =%%
23
24%%[ /* We have a zip code to look up on */
25
26if Not Empty(@zip) then
27
28/* Lets find our zip code in the ShipTimes Data Extension */
29Set @rows = LookupRows("ShipTimes","zip",@zip)
30
31/* We found one row of data */
32if RowCount(@rows) == 1 then
33
34/* Set the row - This now exposes all columns to our variable */
35Set @row = Row(@rows,1)
36
37/* Set the City */
38Set @city = Field(@row,"city")
39
40/* Set the State */
41Set @state = Field(@row,"state")
42
43/* Set the @ship variable to the ShipTime column */
44Set @ship = Field(@row,"ShipTime")
45
46endif
47
48endif ]%%City:%%= v(@city) =%%
49
50State:%%= v(@state) =%%Zip:%%= v(@zip) =%%
51
52%%[ /* We found a record for the zip code */
53
54if not empty(@ship) then ]%%
55
56Shipping Time:%%= v(@ship) =%% %%[ if @ship > 1 then ]%%days%%[ else ]%%day%%[ endif ]%%
57
58%%[ /* We either didn't have a zip code or couldn't find one, prompt for profile update */
59
60else ]%%
61
62We couldn't find a shipping preference. Please
63
64%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%
65
66update your profile
67
68%%[ endif ]%%
69
70Member Preferences
71
72Shopping Preference:%%= v(@mempref) =%%Platinum Member:%%[ if @plat == "N" then ]%%
73
74%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%
75
76Sign-Up %%[ else ]%%%%= v(@plat) =%%%%[ endif ]%%
77
78
79%%[ /* Declare more variables that will be used specifically for our products */
80Var @prodID, @prodName, @prodDesc, @price, @indicator, @color, @totalPurchases, @decimalChar
81
82/* Zero out our total purchases amount */
83Set @totalPurchases = 0
84
85/* Set our CSS color indicator to odd */
86Set @indicator = "odd"
87
88/* Find all the recent purchases for the subscriber we are processing */
89set @rows = LookupRows("Purchases","MemberID",@memID)
90
91/* Make sure we've found some rows before we show any purchase information */
92if RowCount(@rows) >= 1 then ]%%Recent Purchases
93
94%%[ /* Now that we've found some data we can do something with it */
95
96for @i = 1 to RowCount(@rows) do
97
98/* Set the row to the row we're currently looping through */
99Set @row = Row(@rows,@i)
100
101
102/* Set some variables */
103Set @prodID = Field(@row,"ProductID")
104Set @prodName = Field(@row,"ProductName")
105Set @prodDesc = Field(@row,"Description")
106Set @price = Field(@row,"Price")
107Set @totalPurchases = Add(@totalPurchases,Field(@row,"Price"))
108
109/* If the CSS indicator is set to odd use a white background color */
110if @indicator == "odd" then
111Set @color = "#FFFFFF"
112Set @indicator = "even"
113
114/* If the CSS indicator is set to even use a gray background color */
115else
116Set @color = "#EEEEEE"
117Set @indicator = "odd"
118endif
119
120/* Call in our recent purchases content box */ ]%%
121%%= TreatAsContent(ContentAreaByName("my contents\101-401\RecentPurchases")) =%%
122
123%%[ next @i ]%%
124
125%%[ /* Find out if there are two characters after the decimal point */
126
127Set @decimalChar = Subtract(Length(@totalPurchases),IndexOf(@totalPurchases,"."))
128
129
130/* We found only one character after the decimal point */
131
132if @decimalChar == 1 then
133
134Set @totalPurchases = Concat(@totalPurchases,"0")
135
136/* No decimal point was found */
137
138elseif @decimalChar == Length(@totalPurchases) then
139
140Set @totalPurchases = Concat(@totalPurchases,".00")
141
142endif ]%%Total Purchases:$%%= v(@totalPurchases) =%%
143
144
145%%[ endif ]%%このコードの最初の部分は AMPscript 301 にあるコードと同じですが、2 番目の部分は購読者の過去の購入と総支出に関する情報をメールに挿入します。まず、コードは商品および価格情報をメールに挿入するために使用する新しい変数のセットを作成します。
1%%[ /* Declare more variables that will be used specifically for our products */
2Var @prodID, @prodName, @prodDesc, @price, @indicator, @color, @totalPurchases, @decimalChar次に、@totalPurchases 変数と @indicator 変数が値を受け取ります。@totalPurchases を使用して購読者の合計購入額をドルで保存し、@indicator を使用して各購入の背景色を交互に表示します。
1/* Zero out our total purchases amount */
2Set @totalPurchases = 0
3
4
5/* Set our CSS color indicator to odd */
6Set @indicator = "odd"変数が初期値を受け取ったら、Purchase データエクステンションを調べて、メンバー ID に基づいて購読者の以前の購入を検索します。
1/* Find all the recent purchases for the subscriber we are processing */
2set @rows = LookupRows("Purchases","MemberID",@memID)
3
4If the code finds any rows in the data extension, the code uses the following for loop to check each row found:
5/* Make sure we've found some rows before we show any purchase information */
6if RowCount(@rows) >= 1 then ]%%Recent Purchases
7
8%%[ /* Now that we've found some data we can do something with it */
9for @i = 1 to RowCount(@rows) do
10
11/* Set the row to the row we're currently looping through */
12Set @row = Row(@rows,@i)ループの条件に行が一致する限り、コードはこれらのアクションを実行します。まず、以前に宣言した変数の一部に値を割り当てます。Field() 関数はその特定の場所のフィールドに値を設定し、Add() 関数は個々のアイテムの価格を @totalPurchase 累計値に追加します。
1/* Set some variables */
2Set @prodID = Field(@row,"ProductID")
3Set @prodName = Field(@row,"ProductName")
4Set @prodDesc = Field(@row,"Description")
5Set @price = Field(@row,"Price")
6Set @totalPurchases = Add(@totalPurchases,Field(@row,"Price"))また、前のエントリの背景色に応じて、背景色を白またはグレーに設定します。奇数のエントリは背景色が白になり、偶数のエントリは背景色がグレーになります。
1/* If the CSS indicator is set to odd use a white background color */
2if @indicator == "odd" then
3Set @color = "#FFFFFF"
4Set @indicator = "even"
5
6/* If the CSS indicator is set to even use a gray background color */
7else
8Set @color = "#EEEEEE"
9Set @indicator = "odd"最後に、コードは情報を受け取り、引用符で囲まれたフォルダーの場所で参照されるコンテンツブロックと同じ方法で情報を書式設定します。その後、次の行に進みます。すべての行を使用したら、合計購入額の計算に進みます。
1/* Call in our recent purchases content box */ ]%%
2%%= TreatAsContent(ContentAreaByName("my contents\101-401\RecentPurchases")) =%%
3%%[ next @i ]%%コードの最後の部分は、@totalPurchase の小数点以下 1 桁の値の後に 1 つの 0、または @totalPurchase. の整数値の後に 2 つの 0 が表示されるようにします。また、適切なフィールドに @totalPurchase の値も出力します。
1%%[ /* Find out if there are two characters after the decimal point */
2Set @decimalChar = Subtract(Length(@totalPurchases),IndexOf(@totalPurchases,"."))
3
4/* We found only one character after the decimal point */
5if @decimalChar == 1 then
6Set @totalPurchases = Concat(@totalPurchases,"0")
7
8/* No decimal point was found */
9elseif @decimalChar == Length(@totalPurchases) then
10Set @totalPurchases = Concat(@totalPurchases,".00")
11endif ]%%Total Purchases:$%%= v(@totalPurchases) =%%