Og:url Open Graph Dynamic Link - How do you add this so as to query how many shares something has had?

Hello Gang, I am using sumo.com on our site. Some of the share counters work and some dont on our articles.

The Good, the Bad and the Eggly - Smashing the Myth that Eggs Are a Health Food is an example of one that does

And here is one that does not 5 Surprising Statistics That Prove Veganism Is Growing Around The World

Sumo have advised me this:

've noticed there are no og:url meta tags defined on the pages you have linked to me. Share actually uses those og:url tags to lookup the exact URL & ask the social networks for the share count of that specific URL.

If you setup your og:url meta tag properly, Share will automatically read it & give you the exact number provided by social networks.

However; I cant see how I add the URL of the post in when editing the settings page of posts.

There appears to be no setting for the URL?

Do I need to add it to the < head > if so what do I add as the link? Just the root of the site? Or is there a bit of dynamic code that picks up the /posts/titleofthepostandarticle bit?

Thank you

2 Likes

@laveritalondon

Yes the idea you have is partial solution already. What you need to do is copy the following to the <head> area of your posts template;

<meta property="og:url" content=" https://www.plantbasednews.org/post/5-surprising-studies-that-prove-veganism-is-growing-around-the-world" />

You will then highlight the last part of the url (5-suprising-…) and click add field and select slug as shown below ;




You should end up with this and you should be good to go;

7 Likes

Alex, you legend thank you so much!

1 Like

Very welcome and glad it helped.

Hey Alex,

This is a great solution for CMS items! Thanks for sharing.

Do you know if there is a way to do this for normal pages outside of CMS items?

Hi @Max_Hammock_ES

I am glad you found this helpful.

You’ll need to manually set each page link in the meta tag as explained below since at the moment, there isn’t a way to dynamically populate content in static/non-CMS pages;

Thank you so much! In 2023 it somehow doesn’t automatically add OG Url

1 Like

Here’s a solution that works for ALL pages that you can put in your site’s Settings → Custom Code → Head Code section.

It reads the page’s URL then adds it as a new:

<meta property="og:url" content="https://www.yoursite.com" />

<!-- Make sure to add the script tags if you don't already have them in your header -->
<script>
   //  Get the URL from the browser
   const urlPath = window.location.href;
   //  Check for the meta og:url tag
   const ogURL = document.querySelector('meta[property="og:url"]');
   //  Add the meta og:url tag if not found
   if (!ogURL) {
     const meta = document.createElement('meta');
     meta.setAttribute('property', 'og:url');
     meta.setAttribute('content', urlPath);
     document.head.appendChild(meta);
   }
</script>

Here’s an updated script that removes any query-params etc for canonical link:

<script>
   // Get the URL from the browser without query parameters and fragments
   const urlPath = window.location.origin + window.location.pathname;

   // Check for the meta og:url tag
   const ogURL = document.querySelector('meta[property="og:url"]');

   // Add the meta og:url tag if not found
   if (!ogURL) {
     const meta = document.createElement('meta');
     meta.setAttribute('property', 'og:url');
     meta.setAttribute('content', urlPath);
     document.head.appendChild(meta);
   } else {
     // If the og:url meta tag already exists, update its content
     ogURL.setAttribute('content', urlPath);
   }
</script>
1 Like

Has anyone proven that changing meta tags with JavaScript works? My understanding is that crawlers scrape the contents of the page that is served by the server; It doesn’t run JS to evaluate the meta tags.

The solutions provided do not work for me

The JS solution doesn’t work for me also. The first solution works but with Localize, you cannot change the code and adapt for the sub language like fr.mysite.com. So is not a solution.