25 - November - 2016

Social Media Cards: Metatag and semantic data

Post by Andy R

One requirement for our new Drupal 8 site was to implement the necessary markup to provide enough semantic data for services like Twitter, LinkedIn and Facebook to grab the appropriate node data (title, body and image) when sharing posts.

The Twitter developers documentation states that "with Twitter Cards, you can attach rich photos, videos and media experience to Tweets that drive traffic to your website. Simply add a few lines of HTML to your webpage, and users who Tweet links to your content will have a "Card" added to the Tweet that's visible to all of their followers." Facebook have a similar feature and corresponding documentation, as do LinkedIn.

Fortunately with the Metatag module enabling this functionality in Drupal 8 was a relatively painless task! All that was required was to install and enable the Metatag and Metatag: Twitter Cards modules (we're using 1.0-beta10 at the time of writing) and configure some basic, content-global settings.

Twitter card metatag configuration

Essentially, we ensure the card type is set to "Summary card" and that the description uses the [node:summary] token. We also want to provide an image for the summary card. We could provide the "Image URL" in the Twitter card setup which defines the twitter:image meta tag, however Facebook and LinkedIn use Open Graph for cards and we’d also have to provide the og:image metatag too.

This would cause two meta tags to be present in the header containing the same image URL, wasting bandwidth. Luckily, Twitter will fall back to the Open Graph tag if the twitter:image tag is not present so we only have to configure og:image tag.

We also configured several other Open Graph meta tags to support Facebook and LinkedIn.

Global content metatag configuration

We have only configured a subset of the tags available, but this gave us the basic card functionality we were after.

There are tools available to aid in development - allowing you to validate the meta tags and how they will render on social networks.. Your site has to be publicly accessible for these to work.

There are plenty of other guides available if you want more details about what you can do with the Metatag module, try these links:

Profile picture for user Andy R

Andy R

Add new comment

Share this article

Our thoughts

Let's work together

Get in touch and find out how we can empower your organisation.
Back to top