Ask A Question

Notifications

You’re not receiving notifications from this thread.

How can I integrate Amazon Native Ads

Alex Deering asked in General

Im having troubles integrating Amazon Native Ads into my rails apps. I think its a problem with turbolinks but not really sure. Amazon says to just past this code into your html wherever you want it. So I have this:

<div class="row ad-row" id="ad-row" data-search="<%= @recipe.equipment_list.present? ? raw(@recipe.equipment_list.map{|equipment| equipment}.join(", ")) : "cookware" %>">
      <% equipment = @recipe.equipment_list.present? ? raw(@recipe.equipment_list.map{|equipment| equipment}.join(", ")) : "cookware" %>
      <script type="text/javascript">
        amzn_assoc_placement = "adunit0";
        amzn_assoc_search_bar = "true";
        amzn_assoc_tracking_id = "livingrecipe-show-recommendation-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 Required Equipment";
        amzn_assoc_default_search_phrase = '<%= escape_javascript(equipment) %>';
        amzn_assoc_default_category = "All";
        amzn_assoc_linkid = "78aa42b54ebf16f31c5239f132e3d7a0";
        amzn_assoc_rows = "1";
      </script>
      <script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US"></script>
    </div>

When the page is rendered it doesnt show up and this is what it outputs when I inspect it:

There are no errors anywhere on the page. Let me know if you need any more info but this is all I can see that is involved and im probably missing something easy

Reply

Hey Alex,

I haven't actually played with this myself, but you may need some adjustments to make this work with Turbolinks, although you also might not.

Generally if you're putting script tags in the body, Turbolinks will make sure they get executed on the second pageview. You can put a console.log inside the tag that sets all the variables there and see if it prints out when you navigate around.

Since this stuff looks like you're putting it inside the body tag, you should be fine but you may have to make some adjustments.

Can you try putting a console.log in that first script tag and make sure that one prints out each time?

Reply

Added this: console.log("Placement:" + amzn_assoc_placement); and it prints out everytime I load the page. I have added data: {turbolinks: "false"} to the link that loads the page to try and prevent turbolinks interference but that didnt do anything. Heres some more info from the Network tab on inspection:

General
Request URL:http://z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US
Request Method:GET
Status Code:200 OK (from memory cache)
Remote Address:52.84.246.223:80
Referrer Policy:no-referrer-when-downgrade
Response Headers
Access-Control-Allow-Origin:*
Age:35951
Cache-Control:public,max-age=86400,s-maxage=86400,no-transform
charset:UTF-8
Cneonction:close
Content-Encoding:gzip
Content-Length:7672
Content-Type:application/javascript;charset=UTF-8
Date:Mon, 11 Sep 2017 06:42:32 GMT
Expires:Tue, 12 Sep 2017 06:42:32 GMT
Pragma:Public
Server:Server
Via:1.1 b04a4cffa8fb4f524ff7edcab1b5ae31.cloudfront.net (CloudFront)
X-Amz-Cf-Id:RfWJF4SEqMeutE8TgFylnbBxXEgLOFCTtLAmhGGTd1ggfCLWEeu6Nw==
X-Cache:Hit from cloudfront
Request Headers
Provisional headers are shown
Query String Parameters
view source
view URL encoded
MarketPlace:US

There is a response in the network tab as well, I was going to paste it but it is quite long.

Reply

Well, that's sorta good. We know it's running and it's hitting the URL so both of those tags are running.

Do you have a link to a page that you can share with this on it? I was going to poke around and see what's going on. I haven't ever used their ad widgets before myself so I'm not sure what to expect.

Reply

Sure, here: https://www.living-recipe.com/recipes/apple-pie the ad should be showing up just below the ingredients and direction and above the tags looking for this div:

<div class="row ad-row" id="ad-row" data-search="Tart Pan, Sous Vide" data-original-title="" title="">
      <script type="text/javascript">
        amzn_assoc_placement = "adunit0";
        amzn_assoc_search_bar = "true";
        amzn_assoc_tracking_id = "livingrecipe-show-recommendation-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 Required Equipment";
        amzn_assoc_default_search_phrase = 'Tart Pan, Sous Vide';
        amzn_assoc_default_category = "All";
        amzn_assoc_linkid = "78aa42b54ebf16f31c5239f132e3d7a0";
        amzn_assoc_rows = "1";
      </script>
      <script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US"></script><div id="amzn_assoc_ad_div_adunit0_0" data-original-title="" title=""></div>
    </div>
Reply

So I did notice that the response is encoded in gzip. I ran into an issue with searchkick recently and had to include FaradayMiddleware::Gzip in the searchkick initializer. I tried the same thing in the application initializer but it thru some errors. Is there something I should put in the initializer to handle compressed responses

Reply

I don't think that gzip has anything to do with it since it's all controlled by Amazon.

Are you able to paste that Javascript into another site somewhere and see if it runs? I tried to inject the snippet on to a page and didn't see anything at all show up. I'm wondering if it's something to do with your Amazon snippet.

Reply

I had someone else try it on a non rails webpage (wordpress) and it loaded just fine. I will say that Im having the same issue with Google Adsense ads. Where it looks like they are loading and the script runs but its not showing up on the page. Those have currently been removed cause it effects the layout

Reply

Try and google for some answer bro..

Reply

Thanks Tolase, I never wouldve thought to google for the answer. Such insight and amazing help....why not try to be helpful instead of saying something that isnt helpful to anyone. Of course Ive tried searching for answers, but nothing comes up that is the same or fixes my issue. Kinda hard with no error to narrow down what exactly the problem is, so came to a rails community for advice.

Reply

@Alex - Did you ever find a solution to this? I'm running into the same issue. I've found that if you load the page directly, the Amazon ads load just fine. If you navigate to the page via Turbolinks, the ads will not appear.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.