Fix for Drupal 'Social Share Counter' module - Facebook Counter Not Working

Updated: 16 July, 2019 seen 132

I have been using Drupal's Social Share counter module for some time already. It worked great until recently it stopped and all posts were showing 0 shares.

After a quick research on the modules issue page, I found the solution: Facebook share counter not working

Facebook has changed its API recently, so the URL http://graph.facebook.com/?id=URL, which is used by the module no longer works and the share counter is always 0.

There is also a patch provided to fix this issue:

diff --git a/sites/all/modules/contrib/social_share_counter/social_share_counter.inc b/sites/all/modules/contrib/social_share_counter/social_share_counter.inc
index cd85c09b4..48cf292da 100644
--- a/sites/all/modules/contrib/social_share_counter/social_share_counter.inc
+++ b/sites/all/modules/contrib/social_share_counter/social_share_counter.inc
@@ -1,5 +1,7 @@
 <?php
 
+define('FACEBOOK_API_URL', 'https://graph.facebook.com/?id=%&fields=og_object{engagement}&format=json');
+
 /**
  * @file
  * File contains functions for individual share count from all Social service.
@@ -11,10 +13,11 @@
  */
 function get_count_facebook($url) {
   $count = 0;
-  $response = _social_share_counter_parse("http://graph.facebook.com/?id=" . $url);
+  $url = str_replace('%', $url, FACEBOOK_API_URL);
+  $response = _social_share_counter_parse($url);
   $result = json_decode($response);
-  if (isset($result->share)) {
-    $count = formatNumberAbbreviation($result->share->share_count);
+  if (isset($result->og_object->engagement->count)) {
+    $count = formatNumberAbbreviation($result->og_object->engagement->count);
   }
 
   return $count;

After patching social share counter was back and counting. Hope it helps

about

Hi! My name is Reinis Fischer (38), a proud dad and devoted husband. CEO and Founder of Terramatris crypto hedge fund, drone enthusiast, world traveler, photographer, and passionate lover of Georgian cuisine (vegetarian).

An ex-pat living in Georgia since 2011, I trade stocks, take photographs, work out at the gym, and many more. Here I write about travel, finance, and other things that might interest me.