Dynamically changing OG tags

#1

Hi there,

So I’m looking to setup my website which has multiple pages but I want these to be independently shareable on Facebook - this of course (it appears!) means that I need to setup OG tags for the Facebook share button to pick up on.

However, as I will have multiple pages to look after my original thought was to use JavaScript to simply pick up on various elements on the page to populate the OG tags (e.g. title, description, image). After some initial tests I’ve found that the OG tags are not being updated within the source, but I presume that this will also be an issue if Facebook were to pick up on the OG tag details?

Is it possible to use JavaScript to dynamically alter OG tag contents or would this need to be done on the server-side? Does anyone have any other recommendations of how to make my content shareable but the shared content to be specific to each page (e.g. the title and image that’s shared)?

Many thanks in advance!

#2

So after finding the Facebook Sharing Debugger tool, I’ve found that none of the OG values are being changed by the JS - I assume then that this isn’t possible with JS?
https://developers.facebook.com/tools/debug/sharing/?q=http%3A%2F%2Fwww.andrewcourtney.co.uk%2Ftest%2Fproject-test.html

#3

I think it’s that Facebook gets the OG data and doesn’t run any DOM changing JavaScript. i.e. Facebook is not a browser.

It might be possible to write some kind of code that runs on a FaceBook page, but I think any dynamic OG data script should run before it’s sent to FaceBook.

#4

If I interpret your question correctly, you can construct the meta tags with JS certainly, but there’s no point really because the way it works the tag has to be present on the page even before the page has loaded. Javascript adds the Meta tag to the page after DOM has loaded. Meta tags have to be on the page or implemented server side.

hope this helps.

2 Likes
#5

Right…that makes sense and as I thought. So it would have to be done on a back-end point of view I would guess?

Thanks also Whittfield - that’s as I thought.

On a slightly different topic, if I were to use JS to populate title attributes on links in a page, would these be used for SEO purposes or would Google etc. discount them as they would be loading after page-load?

Thanks!