'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.
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?
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;
<!-- 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>
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 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.