Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Facebook Open Graph tags possible?

Tags:

I'm dynamically changing my <meta property="og:image" content="#"> and <meta property="og:title" content="#"> tags using jQuery (below). When I view the code via 'inspect' in Chrome, the tags have successfully been changed.

HTML:

<meta property="og:title" content="#">
<meta property="og:image" content="#">

jQuery:

$("meta[property='og:title']").attr("content", data.name);
$("meta[property='og:image']").attr("content", data.thumbnail.url);

But the Facebook debugger tool is still showing content="#" for each. I'm assuming this is because Facebook reads the source code, before Javascript has a chance to replace the content.

Is there a way around this?

Thank you.

like image 753
user1661677 Avatar asked Dec 15 '15 05:12

user1661677


People also ask

Does Facebook use Open Graph?

Open Graph MarkupMost content is shared to Facebook as a URL, so it's important that you mark up your website with Open Graph tags to take control over how your content appears on Facebook. For your website to be shared correctly by our crawler, your server must also use the gzip and deflate encodings.

How do you put tags on an open graph?

Just go to Page Settings > Social Image > Upload. If you need to add other OG tags and customize the default settings, go to Page Settings > Advanced > Page Header Code Injection. Read the following section on adding the tags manually and copy-paste the code there.

What property tags are used for open graph?

The four basic open graph tags that are required for each page are og:title , og:type , og:image , and og:url .

How do you add an Open Graph to Facebook?

Click on 'Social'. Click on the 'Facebook' tab. Toggle the 'Add Open Graph meta data' switch. To enable the feature, toggle the switch to 'On'.


2 Answers

Facebook does not parse JavaScript at all, you can´t use dynamic Open Graph tags. It does not really make sense to change them on the fly anyway.

You can only change the OG tags dynamically on the server - obviously. For example: https://yourdomain.com/dynamicogtags.php?title=xxx&description=xxx

<meta property="og:title" content="<?php echo $_GET['title'];?>">

Not sure if that´s what you want to do though, the URL looks pretty ugly that way. Rewrite would be nice, of course.

You also may want to try something like prerender.io, but i am not sure if it will handle dynamic og tags.

like image 105
andyrandy Avatar answered Oct 01 '22 15:10

andyrandy


As said previously, Facebook doesn't parse JavaScript at all.

One way of doing it (the way I do it) is to use a prerendering service like prerender.io to prerender your pages and redirect requests coming from web crawlers to that prerendering server based on the user-agent (you can easily find how to do that with your Nginx/Apache server on google).

Prerendering services produce an HTML/CSS render of your page, but they wait until the page is fully loaded and the JavaScript is executed before doing so. That way, Facebook gets a render of your website where the JavaScript has been executed and the OpenGraph tags are set correctly!

Prerender is open-source so you can run your own prerender server for free!

like image 34
Martin Vandersteen Avatar answered Oct 01 '22 16:10

Martin Vandersteen