Hello,
I want to have an Amazon search ads widget on a blog page.
When I use the Amazon generated code directly in the “code” section of “HTML iFrame”, it works perfectly.
The code is as follows:
<script type="text/javascript">
amzn_assoc_placement = "adunit0";
amzn_assoc_search_bar = "true";
amzn_assoc_tracking_id = "atmarhoreca-20";
amzn_assoc_search_bar_position = "bottom";
amzn_assoc_ad_mode = "search";
amzn_assoc_ad_type = "smart";
amzn_assoc_marketplace = "amazon";
amzn_assoc_region = "US";
amzn_assoc_title = "Shop Related Products";
amzn_assoc_default_search_phrase = "product";
amzn_assoc_default_category = "All";
amzn_assoc_linkid = "ae6ccb4140ba25fbef6d6ba67d36b98d";
</script>
<script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US"></script>
In corvid, I get the blog post title and send it to the HTML element by:
$w.onReady(function () {
loadSearchAds();
});
async function loadSearchAds() {
let getPostData = await $w('#post1').getPost();
let postTitle = getPostData.title;
$w('#html2').postMessage(postTitle);
}
I must send a message to the HTML element to set “amzn_assoc_default_search_phrase” variable as the post’s title to show search results related to the blog.
I am not so good at JavaScript. Tried the following HTML snippet but couldn’t manage:
<script type="text/javascript">
window.onmessage = (event) => {
if (event.data) {
searchPhrase = event.data;
} else {
searchPhrase = "horeca";
}
};
amzn_assoc_placement = "adunit0";
amzn_assoc_search_bar = "true";
amzn_assoc_tracking_id = "atmarhoreca-20";
amzn_assoc_search_bar_position = "bottom";
amzn_assoc_ad_mode = "search";
amzn_assoc_ad_type = "smart";
amzn_assoc_marketplace = "amazon";
amzn_assoc_region = "US";
amzn_assoc_title = "Shop Related Products";
amzn_assoc_default_search_phrase = searchPhrase;
amzn_assoc_default_category = "All";
amzn_assoc_linkid = "ae6ccb4140ba25fbef6d6ba67d36b98d";
</script>
<script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US"></script>
I hope it is clear. Any help would be appreciated.
Thank you.