Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Svelte and Salesforce Marketing Cloud
Domenik Reitzner
Domenik Reitzner

Posted on

     

Svelte and Salesforce Marketing Cloud

Goal

  • use svelte for a simple questionnaire for a Salesforce Marketing Cloud Cloud Page
  • generate JSON Server Side for questionnaire data
  • submit data with post to Salesforce Marketing Cloud

Use Svelte

To get this up and running it is actually pretty easy.

Start with your standard svelte starter project.

I just defined a custom container as target (#questionaireContainer) and specified the structure of the questionnaire data and it's container (script#questionaireData). The data may vary for your use case, but here is mine:

[{"id":"string","title":"string","question":[{"id":"string","title":"string","position":1,"options":[{"id":"string","value":"string"}]}]}]
Enter fullscreen modeExit fullscreen mode

On startup you just get the data from the script tag and parse it.

constdata:Idata=JSON.parse(document.querySelector('#questionnaireData').innerHTML)[0];
Enter fullscreen modeExit fullscreen mode

Now you can work with that data and implement your style. After building your project you need to go to WebStudio > Cloud Pages

Select Cloud Page

and create new code resources inside one of your collections.

Add new Code resource

Now you can grab that URL and put it asscript andstyle tags into your micro site.

Generate JSON data

We are using a data extension for the questionnaire configuration. We simply made a for loop that goes over each row in our data extension and generates our data in our predefined format.

<scriptid="questionnaireData"type="application/json">%%[set@json=Concat('[{"questions": [')if@questionRowCount>0thenfor@j=1to@questionRowCountdo/* magic here*/nextendifset@json=Concat(@json,@questionJson,']}]')]%%%%=v(@json)=%%</script>
Enter fullscreen modeExit fullscreen mode

Submit data with POST

So this one was a bit tricky to figure out, as the Salesforce documentation is not really forthcoming about how they can handlePOST requests in their "beautiful" scripting language (amp script).

So inside our svelte app we just need to do a post with form values (form submit the js way).

constpostData=()=>{constbody=newURLSearchParams({currentQuestion:question.id,currentAnswer:$selectedStar[step]||'',});constoptions={method:'POST',body,}fetch(data.url,options).catch((err)=>console.error(err));};
Enter fullscreen modeExit fullscreen mode

In the cloud page we just watch for a request parameter (currentQuestion).

if RequestParameter("currentQuestion") !="" then   set @currentQuestion = RequestParameter("currentQuestion")   set @currentAnswer =  RequestParameter("currentAnswer")   set @isPost=true   if RequestParameter("currentAnswer")!="" then      /*Save answer in DE*/   endifendif
Enter fullscreen modeExit fullscreen mode

So now we have a way to get data from the marketing cloud into a frontend framework like svelte and alsoPOST data back into it.

Conclusion

It is really nice to use a frontend framework inside of the salesforce marketing cloud, as it is so much better than the alternative, but it is not as easy as I would desire it to be.

Deployment is currently only possible to be done manually (I might have an idea to work around that in the future, but I'm not there yet).

Another drawback is that writing logic in AMP script is just so cumbersome. Everything that goes beyond reading/writing some data from/to a data extension gets complicated really fast.

I hope you enjoyed the read and could take away something.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Christian, husband, father of four, web-enthusiast, Sveltian, musician and blogger.
  • Location
    Vienna
  • Education
    School for mechatronics with emphasis on robotics
  • Work
    Senior Frontend Developer @ woom
  • Joined

More fromDomenik Reitzner

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp