Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: newsletter api endpoint and payload #400

Merged
merged 14 commits into from
Feb 28, 2024
22 changes: 13 additions & 9 deletions blocks/newsletter-signup/newsletter-signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ async function submitForm(block, fd) {
const formData = new FormData(block.querySelector('form'));
const payload = {
email: formData.get('email'),
dataFields: {
catnewsletter: formData.get('cats') === 'on',
dognewsletter: formData.get('dogs') === 'on',
},
mergeNestedObjects: true,
createNewFields: true,
first_name: formData.get('name'),
catnewsletter: formData.get('cats') === 'on',
dognewsletter: formData.get('dogs') === 'on',
singupdate: new Date(),
};

const apiKey = 'APIEvent-74e121c6-6308-c35e-8320-d335ee59f191';

const fetchOpts = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Api-Key': '3e7a9624572b4827b156af44e72fceaa',
'Api-Key': apiKey,
},
body: JSON.stringify(payload),
};

try {
const res = await fetch('https://api.iterable.com/api/users/update', fetchOpts);
const baseUri = 'https://mcsbqrbj0-1-ng0pyzwy21q8hn78.rest.marketingcloudapis.com/';
const res = await fetch(baseUri, fetchOpts);

if (!res.ok) {
let text = 'no detail.';
try {
Expand All @@ -49,7 +53,7 @@ async function submitForm(block, fd) {
showMessage(block, fd.Success);
pushToDataLayer({
event: 'sign_up',
signup_category: 'newsletter', // Example: 'newsletter'
signup_category: 'newsletter',
});
}
} catch (e) {
Expand Down
Loading