Let’s have a look at the problem below:
I am brand new to AMPScript and have a complicated use case I’m hoping somebody could help with. I have data that is being imported into SFMC that relates to offers that a customer may receive. In that data extension, some customers may receive all 5 offers, while others will only receive one.
Is there a way to show content blocks for individuals who qualify (as listed in the data extension) and collapse and hide content for those that don’t qualify?Someone new to Marketing Cloud
This theoretically sounds like a well defined question for ChatGPT. Why?
- Scripting language being used is AMPScript
- Data is stored in a data extension
- The number of offers is within 1 to 5
- We should show blocks conditionally, based on the offer data
Let’s see how ChatGPT thinks this can be solved?
Yes, you can achieve this using AMPScript in conjunction with Dynamic Content Blocks in Email Studio.
Here’s a high-level approach:
- Create a Data Extension that contains information about the offers and which customers qualify for them. Each row in this Data Extension should have a unique identifier that corresponds to the subscriber key in your email audience Data Extension.
- Create a Dynamic Content Block for each offer. These content blocks should contain the offer details and any relevant images, links, or calls-to-action.
- In your email, use AMPScript to retrieve the subscriber’s unique identifier (e.g., the subscriber key) and look up their offer qualification data from the Data Extension you created in step 1.
- Use conditional statements in AMPScript to determine which Dynamic Content Block(s) to display based on the subscriber’s offer qualification data.
- Place the Dynamic Content Blocks in your email where appropriate, and wrap them in an AMPscript block that uses conditional statements to show or hide them based on the subscriber’s offer qualification data.
Here’s some sample code to get you started:
%%[ /* Step 3: Retrieve offer qualification data for the current subscriber */ SET @subscriberKey = AttributeValue("_subscriberkey") SET @offersDE = "my_offers_DE" /* replace with your Data Extension name */ SET @offer1Qualified = Lookup(@offersDE, "Offer1Qualified", "SubscriberKey", @subscriberKey) SET @offer2Qualified = Lookup(@offersDE, "Offer2Qualified", "SubscriberKey", @subscriberKey) SET @offer3Qualified = Lookup(@offersDE, "Offer3Qualified", "SubscriberKey", @subscriberKey) SET @offer4Qualified = Lookup(@offersDE, "Offer4Qualified", "SubscriberKey", @subscriberKey) SET @offer5Qualified = Lookup(@offersDE, "Offer5Qualified", "SubscriberKey", @subscriberKey) /* Step 4: Use conditional statements to determine which content blocks to show */ IF @offer1Qualified == "true" THEN SET @showOffer1 = true ELSE SET @showOffer1 = false ENDIF IF @offer2Qualified == "true" THEN SET @showOffer2 = true ELSE SET @showOffer2 = false ENDIF /* repeat for offers 3-5 */ /* Step 5: Wrap Dynamic Content Blocks in conditional AMPscript */ ]%% <!-- Offer 1 --> %%[ IF @showOffer1 THEN ]%% %%=ContentBlockbyId("offer1_block_id")=%% %%[ ENDIF ]%% <!-- Offer 2 --> %%[ IF @showOffer2 THEN ]%% %%=ContentBlockbyId("offer2_block_id")=%% %%[ ENDIF ]%% <!-- repeat for offers 3-5 -->
In this example, the code assumes that the offer qualification data is stored in a Data Extension called
my_offers_DE, with fields named
ContentBlockbyId function is used to retrieve the content for each Dynamic Content Block based on its ID. You’ll need to replace
offer2_block_id, etc. with the actual IDs of your Dynamic Content Blocks.
Sounds ok, doesn’t it? Well, let’s discuss why this is not the best solution out there.
But before we start, let’s give some credit to ChatGPT. It got few things right. It looks up the data based on the subscriber key, and selects matching records. And the code seem to be syntactically correct AmpScript.
And here the good things end. What is wrong with this, and why? Or, maybe not directly wrong, but it can indeed be heavily improved.
- ChatGPT suggests using Lookup instead of LookupRows. This requires iterating through the entire data extension 5 times. Especially in this case, where the row returned is exactly the same one in each execution of Lookup function. If this is a Data Extension with hundreds of millions of rows, without proper indexing, and you are using this script in an email sent to millions of customers, it will take significantly longer to process. And hence your sendout might take few hours more than necessary. You don’t want that latency in your life.
- Also, ChatGPT assumes the offers are stored in a single row per subscriber, but in individual fields. I would more naturally assume multiple records per subscriber, with unique offer each. This would allow for adding more than 5 offers in the future, without updating the data extension structure (with another OfferXQualified field.
- Now on to something which might have me kicked out of the computer science class. ChatGPT looks at the value of offerXQualified (which is boolean) and sets the showOfferX to the similar, boolean, value… with an IF THEN ELSE statement… THIS IS WRONG ON SO MANY LEVELS
- Finally, we are doing…. another IF THEN ELSE on this showOfferX value….
and presenting a content block with that particular offer. I am… surprised 🙂
- Also… why did it start with step 3? Where are the first two steps? What are we missing? I am now even more curious.
So, how would I solve this instead?
Well, I would use slightly different assumptions.
- Multiple records per subscriber, each holding unique offer data
- We can reuse the content block, as long as we populate it with unique variables from each row.
Which brings us to the code:
%%[ var @offerRows, @offerRow, @offerRowCount, @subscriber, @i set @subscriber = AttributeValue(_subscriberkey) /* unique ID of the recipient */ set @offerRows = LookupRows("OfferDataExtension","customer_id", @subscriber) set @offerRowCount = rowcount(@offerRows) if @offerRowCount > 0 then for @i = 1 to @offerRowCount do var @offerLink, @offerName set @offerRow = row(@offerRows, @i) set @offerLink = field(@offerRow,"link") set @offerName = field(@offerRow,"name") ]%% %%=ContentBlockByKey("offerContent")=%% %%[ next @i else ]%% Sorry, no offers for you. %%[ endif ]%%
As you see, in my case, the rows are searched only once, and printed using a for-loop. Also, we use only a single content block, as the question does not explicitly require different offer content blocks. Instead two variables are fetched from the lookup and passed to the offerContent content block, so it can show unique offers. It can of course be any variables, not only link and name.
This shows, in a quite interesting way, that ChatGPT or any AI for that matter CAN be correct, and it CAN give you a working solution, but it WON’T teach you best practice. So, next time you want to brush up on your Ampscript or any other skills for that matter, ask a human before you ask a robot.
So, what do you think? Let me know in LinkedIn comments here.