Dumb SEO Questions

(Entry was posted by Kyle Doran on this post in the Dumb SEO Questions community on Facebook, Thursday, April 12, 2018).

Dynamically add product schema to an ecommerce site`s products

I`m currently using GTM to dynamically add product schema to an ecommerce site`s products. Everything is working great, however, I have yet to figure out how to include the product image within the dynamically added schema. I know how to do this for each product individually but I want to add it dynamically, meaning when the page loads and GTM fires the schema it automatically grabs the on page elements to include in that specific products structured data. Like I said I`ve figured out how to do this for all of the other key elements on page besides the product image url. Seems like it would be easy, but it`s proving to be a helluva task. Any help would be much much appreciated! Let me know any details that I can include if what I want to accomplish doesn`t make sense.

This question begins at 00:43:32 into the clip. Did this video clip play correctly? Watch this question on YouTube commencing at 00:43:32
Video would not load
I see YouTube error message
I see static
Video clip did not start at this question


Selected answers from the Dumb SEO Questions G+ community.

  • John Bosworth: How are you adding the product info dynamically?Are you creating selectors and grabbing Dom elements? Can you grab the og:image element you’ve set for that page?
  • Kyle Doran: I`m using DOM element CSS selector to target on page elements. For example I targeted the product title with: h2.product_title.entry-title. I`m sure I`m only missing something simple in trying to target the product image, but I can`t seem to figure it out. I like the idea of grabbing the og:image element, however I`m not quite sure how to go about doing this. I`ve tried using a javascript variable: $(`metaproperty="og:image":eq(0)`).attr(`content`) to no avail. Any ideas? Thanks again for your help :)
  • Tajmul Hossain: Our property comes from partners site dynamically. Is it same things?
  • Kyle Doran: I`m not sure what you mean.. I`m trying to target the product image for schema instead of having to add the url for the image for each product separately.
  • Dave Elliott: You need to either find something unique and common across all the mages you intend to use (class, height, width, folder, image format etc.) Or it needs to be in the same place in the code each time(e.g. image in 3rd div on the page) in which case you can grab it with x-path.
  • Kyle Doran: Thank you for your tip. I`m trying this out today. It looks like x-path doesn`t work with google tag manager. Each product image is located in the same area on each product. I`ve tried tons of different element paths, but I can`t seem to get it correct as my results are always " " or "false" or "null" for the image schema. Would it be correct to target img.wp-post-image? I`ve tried this before without getting the image url back. I`m sure I`m just missing something simple. Any ideas? Thank you again for your help!
  • Kyle Doran: SOLVED! Was able to use a custom javascript function to target the correct image on page. function() {var picture = document.getElementsByClassName("woocommerce-product-gallery__image")0.getElementsByTagName("img")0;var src = picture.getAttribute(`src`);return src; } Thanks again for your help everyone! Much appreciated :)

View original question in the Dumb SEO Questions community on Facebook, Thursday, April 12, 2018).

Reference Links

All Questions in this Hangout