One possible approach is to implement your own referrer-based algorithm, but there are some cases that you should consider. At first glance, here are some of them.
I am sure that there are other pitfalls.
However, you can try some libraries of tracking URLs, such as Google Analytics (for advanced statistics) or Google Short URLs for basic ones.
He already answered Stack Overflow how to get pageview information for specific URLs in Google Analytics .
Here's how the Google Shorten URL works (examples taken by Google Shorten URLs ):
For your URL, you create an abbreviation:
curl https://www.googleapis.com/urlshortener/v1/url \ -H 'Content-Type: application/json' \ -d '{"longUrl": "http://www.google.com/"}'
If the generation was successful, you will receive the following answer:
{ "kind": "urlshortener#url", "id": "http://shortenurl/", "longUrl": "http://www.google.com/" }
Note that the id key is your shortened URL. This way you can save it in your database.
Later you can get URL reduction statistics as follows:
curl 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://shortenurl/fbsS&projection=FULL'
And here are the statistics:
{ "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": { } } }