What Is It?

It is really frustrating when you migrate your popular website from HTTP to HTTPS because you just want to add security, and suddenly your famous website pages lose the massive share counts! “Now what, Shall I be unsecured to be popular?” will be the first thought popping up in your mind. But that is not the solution, we can’t disable SSL.

I decided to keep the website on HTTPS, without worrying about the social status for a few days and spent some time researching this a bit. The problem is very simple,

Social websites treat each URL unique, including the protocol! So, http://y.com and https://y.com will be different!

So obviously the share counts will be different, and also the future shares will be tracked separately. Though I have AddThis to generate social sharing buttons and counters, it actually uses the social network’s support to do that. Then how can we fix it?

How to Fix It?

Each social network treats the shared URL tracking in diff. ways, I have verified this on Facebook and Pinterest and found that they return diff. counts for the same URL but diff. protocols. Somehow LinkedIn has this thing correct!

Facebook

To check facebook’s share count, we can access http://graph.facebook.com/http://y.com, which will fetch the shares for HTTP URL. Further, Facebook reads the page URL from meta tag ‘og: URL’ and ‘canonical’ tag as suggested here and also in facebook’s help pages. But this would only fix the issue for only Facebook, for others we anyway need to find a better way.

After doing much research, I found the Addthis documentation showing a javascript method supported to fetch social share counts for diff. service providers for a given URL.

  • It allows fetching counts for Facebook, LinkedIn, Pinterest, Reddit, Delicious.
  • Twitter is not supported now via Addthis or also directly, so no way to get a twitter share count for any URL!

The Solution

This issue can be solved in diff. ways,

  • I can show all the diff. buttons for diff. services that support dynamic button generation. This would allow fetching the latest share counts, but only for one URL as passed in the button generation code. Like it is shown here
  • I can fetch HTTP and HTTPS URL share counts for all the allowed service providers mentioned above - Facebook, LinkedIn, Pinterest, Reddit, Delicious, etc. - and then show the sum as total share count. But this would also need me to keep all current HTTPS share counts for other services that are directly not supported by Addthis JS library, and this would be complicated.
  • I can just fetch the HTTP share count for the same page for allowed service providers, and add them up in the existing Addthis share count value. Yes, this is it, I just need to add the HTTP share count in the current HTTPS share count and done!

Finally, after evaluating all the possible solutions we decided to take the last approach, Where I can fetch the social share counts for the given page from Facebook, Pinterest, Reddit and Delicious for Http URL and add them up using javascript in the https count Addthis has generated. We would need to do the following steps,

  • Check if the Addthis DOM elements are created, if not then check again after a couple of seconds. This is to ensure that our javascript runs after Addthis JS is executed and the page is loaded.
  • Get current https URL share count generated by Addthis. Now, this could be in 1k or 1m format, so we need to consider to convert the numbers accordingly.
  • Get the Http URL share count by using the Javascript functions from Addthis object itself. This would also need us to wait for a couple of seconds before the Addthis server call fetches the counts, by avoiding the async nature of Javascript.
  • Sum them up, so now we have all the share counts. Note that Twitter and Google+ are not supporting it, so there is no way we can do that using Addthis. But we can at least get some more counts from our previous URLs.
  • Check if the total share count is in a multiplication of 1k or 1m, and format the number accordingly so that we can display a bigger number in a small space if needed.

Finally, we have retained the Http URL share counts, no changes in the configurations of AddThis OR meta tags are needed.

Sometimes, Javascript can help where nothing else seems to help, so keep scripting!

Click here for more blogs

At BoTree Technologies, we build enterprise applications with our Development team of 70+ engineers.

We also specialize in AI, ML, Ruby on Rails, Python, Django, Spree commerce, NodeJs and ReactJS.

Consulting is free – let us help you grow!