One possible approach is implementing your own algorithm based on the referrer, but there are some cases these you have to take in account. On a first thought here are some of them.
- How to determine unique clicks? (IP, Browser data or combination of both?)
- Is the referrer unique? (https://www.facebook.com/, https://m.facebook.com/)
I'm sure that there are other pitfalls too.
However you can try some tracking URL library, for example Google Analytics (for advanced statistics) or Google Short URL for a basic ones.
It's already answered on Stack Overflow, how to get page view information for specific URLs in Google Analytics.
Here is how Google Shorten URL works (the examples are taken by Google Shorten URL Docs):
For your URL you generate a shorten one:
curl https://www.googleapis.com/urlshortener/v1/url \
-H 'Content-Type: application/json' \
-d '{"longUrl": "http://www.google.com/"}'
If generation is successful, you will receive the following response:
{
"kind": "urlshortener#url",
"id": "http://shortenurl/",
"longUrl": "http://www.google.com/"
}
Please note that id key is your shorten URL. So you can store it in your Database.
Later you can get your shorten URL statistics by the following call:
curl 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://shortenurl/fbsS&projection=FULL'
And here are the stats:
{
"kind": "urlshortener#url",
"id": "http://shortenurl/",
"longUrl": "http://www.google.com/",
"status": "OK",
"created": "2009-12-13T07:22:55.000+00:00",
"analytics": {
"allTime": {
"shortUrlClicks": "3227",
"longUrlClicks": "9358",
"referrers": [ { "count": "2160", "id": "Unknown/empty" } /* , ... */ ],
"countries": [ { "count": "1022", "id": "US" } /* , ... */ ],
"browsers": [ { "count": "1025", "id": "Firefox" } /* , ... */ ],
"platforms": [ { "count": "2278", "id": "Windows" } /* , ... */ ]
},
"month": { /* ... */ },
"week": { /* ... */ },
"day": { /* ... */ },
"twoHours": { /* ... */ }
}
}