I want to show an alert message to the customers that are landing on my shopify store. So I have written an endpoint to post a scripttag that is containing javascript code to show alert.
Here is api endpoint:
app.post("/api/script/add/script_tags.json", async (req, res) => {
let status = 200;
let error = null;
try {
console.log('script_taggggggggggggggggggggggggg',res.locals.shopify.session);
const script_tag = new shopify.api.rest.ScriptTag({session: res.locals.shopify.session});
script_tag.event = "onload";
script_tag.src = "https://raw.githubusercontent.com/Sadiquzzaman/test/main/text.js";
await script_tag.save({
update: true,
});
res.status(200).send(script_tag);
} catch (e) {
console.log(`Failed to add script tag: ${e.message}`);
status = 500;
error = e.message;
res.status(200).send(e);
}
});
I have integrated this from my frontend. here it is:
const response = await fetch("/api/themes/update/"+id,
{
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
value: value
})
}
);
console.log(await response.json());
Here I can make sure that scripttag is adding successfully. Problem is it is not showing up to my store when I am viewing my store as a customer. As an add on: "https://raw.githubusercontent.com/Sadiquzzaman/test/main/text.js" this file contains code:
alert("Hello! Welcome to our shop.");