Short URL Outbound Link Tracking in PHP and MySQL - php

I have a site that shortens links based on Noah Hendrix's tutorial on the subject. I decided that it would be great if I could track when users click the short URLs, similar to the way that HootSuite users can track their links with Owly. I currently have a database which has the short URL stored along with the true URL and it's click count. Ideally the click count column would update when that short URL is accessed by an outside user.
In short, I am looking for a PHP/MySQL solution to keep track of the number of times various short URLs are clicked. Any additional information that could be gathered from the clicks would be greatly appreciated as well.

I am assuming you followed the php version of his tutorial. If so look at the listing for serve.php under "Serving the Short URL". In the section round line 11 where it sets the 301 status you can log the redirect there with an update to the database. Something like
$query = mysql_query("update `".$database."`.`url_redirects` set count=count+1 where `short`='".mysql_escape_string($short), $db);
$row = mysql_execute_update($query);
should do it.

Here's a round-about alternative--How about a non-brain damage approach? Try putting Google Analytics on the site. You'll not only get a click report, but can also track paths through the site, ins vs outs, network properties, user locations, etc. It's a simple javascript include, and takes all of 5 minutes to setup start to finish.
I've been a PHP developer for a long time, and my personal theory is that there's a lot of challenges that need to be solved out there, no reason to waste time on solutions others are willing to give you...

Related

PHP: How to detect where user is going out from my website

I have strange requirement and I am not able to find dour whether it is feasible or not. Business wants to know outgoing activity from user. like from our website, where user goes out?
For example: user comes on my website (source): www.DummyTest123.com. After 5 mins of surfing, he goes out to (destination) www.Google.com or www.AnyOtherWebite.com.
How do I know can capture ( www.Google.com or www.AnyOtherWebite.com.) in my Database.
What are the classes or methods available in php to accomplish above requirement?
PS: http_referer will not work as in that , my website is destination.
Thanks
If you have outgoing links on your site to those places, you can change them to point to a page on your site that logs where they're going then redirects them to their destination.
Other than that - nope. That information isn't provided to you in any way.
It is not possible to check what website are people visited after they left yours unless they actually click on the link which is located on your website, and then get redirected.
This is only somewhat possible, and most likely not a great idea to implement. To answer your question, though, you could potentially have an outbound route on your site in which you direct all outbound links to:
http://www.yourdomain.com/outbound?r=www.google.com
There you can log the outbound activity to your hearts content.
There are, however, at least a couple drawbacks.
All outbound links will need to link to this URL. If you have a lot of existing content, it can take quite awhile to update all references to use the new link.
Users may trust your site less, as its not clear to them why you would not just link directly to the page they're trying to go to. What data are you collecting? Why are you collecting it? These are just some of the questions that may run through their minds.
Keep in mind, that this does nothing to log users who go up to the URL bar and physically type in a different URL, or open a new tab then close the one with your site.

setting up a database for tracking which users have clicked which links?

I'll try and elaborate on my problem a bit more.
I recently got an entry-level part time developer position with my university, in efforts to sharpen my dev skills. While I have used MySQL in the past, it was only briefly covered in a single course, as I am mostly a front end guy (HTML/CSS/JS).
Anyways, the department that hired me has a website designed for incoming students, to get them acclimated to college. It has tutorials and videos for them to watch, etc. In order to access the site, they must log in to their university account (which uses LDAP). Account names are in the format of abc1234.
Now, my problem is I need to create a way for the staff to track which tutorials/videos the freshman have watched. They would like me to do this using databases. There will potentially be thousands of students, and they want to be able to see exactly which students have / have not clicked each link/watched each video.
How should I set up databases for this? There will be multiple links/tutorials/videos that they want to track. Bonus points if there was a way of tracking which users watched the videos through to the end, however not required.
I believe I will need to use PHP for handling the exchange between the browser and the database, correct?
Thank you for any helps or tips. :)
You could simply make a PHP script that would retrieve the requested link for them, while also adding a value to MySQL.
If I were going to do this, I'd probably do something like this:
Video 1
And in PHP, I'd simply get the resource, type and the userid from the session, put them into the database, then retrieve the resource they were looking for. To track if they watched the video the entire way through, you could use javascript to watch for the event of the player coming to an end, then just use a skin that doesn't have a scrub bar.
you need for example a table "users: id, name, etc..." and a table "clicks: user_id, url".
to track link clicks you could do something like this:
<a hreF="log_click.php?url=<?php echo urlencode("some_url?some=param&etc=anything"); ?>">
log_click.php
<?php
$url = $_GET['url'];
$user = /* ie. $_SESSION['user_id'] */
/* insert to database */
header('Location: '. $url); // maybe need urldecode($url) here
exit;
?>
Interesting project. You should extract the user from the current accesed page and store in a visited pages log (within the database) for each user.

php how to know that a click came from google

My adsense ad have a dedicated land page.
I want to show the content only to those who came through that ad.
The page is coded with PHP so I'm using $_SERVER['HTTP_REFERER'].
Two questions here:
Is there a better alternative to $_SERVER['HTTP_REFERER'] ?
To what strings/domains should I compare the referrer's domain (I'll handle extracting it)? I mean, I'm guessing that google has more than one domain they're using for the ads, or not? There's doubleclick.com.... any other domain? How can I check it, besides try/fail?
$_SERVER['HTTP_REFERER'] is the canonical way to determine where a click came from generally. There are more reliable (and complicated) methods for clicks within a site you fully control, but that's not much help for clicks from Google. Yes, it can be spoofed, and yes, it can be null, but as long as you're not targeting nuclear weapons based on that data, and you can handle null values gracefully, it should be good enough.
As for domains, you have to consider the international google domains, as well as all the google*.com domains.
I suggest adding a parameter on the link you give to Google. i.e. instead of yoursite.com/landing, do yoursite.com/landing?campaign=12.
If you are concerned that curious users will play with this parameter, the fix is simple-- redirect via a server 301 redirect when they hit that URL.
That is, if I request yoursite.com/landing?campaign=12, your server--before serving a page-- should log my visit to campaign 12 and redirect me to the plain url yoursite.com/landing. This has the added advantage that reloads won't increment your campaign hit count.
Yes, users could still mess with the original link if they are clever or curious enough to look at it before they click on it, but I think this is going to be far more effective than sniffing the referer.
Rather than trying to work out on your own how to measure your page views, you can consider using an existing system for that, like Google Analytics

PHP Redirector Counter for a link

I need a way to count how many times a link is being clicked and I was thinking of creating a php script to redirect to and do the counting. Is there a better way to do this and how would i count each time the user visits the link and would it be best to save in the database somewhere...any suggestions
Yes, it must be a PHP script - JavaScript for example won't work all the time.
So - instead of a link to
http://some.site.com/page2.php
You would link to
http://some.site.com/redirect.php?page2.php
And in the redirect.php you will track, for example, in a database, the values, and in the end throw this header:
header("Location: http://some.site.com/".$_SERVER["QUERY_STRING"]);
To redirect to the path after ?...
// yeah - logs might work... a little bit more work, though and it is also very server specific.
I would analyze your web log files as this will work whether it's a static page or a script.
If the page you need to count is a script, you could insert code that updates a table.
Website statistics is a big industry and there are many free and pay solutions out there to explore and get ideas from.
If you need to track clicks on a specific link then you'll probably need to use javascript to capture the click and send a notification to a tracking server. If you need to track page views then you're best off looking at your server logs. Remember that a page can have many links pointing to it, you have to differentiate between link clicks events page page impressions. Another possibility, depending on your application, is to use Google tracking, or a similar third party tracking app.

data between pages: $_SESSION vs. $_GET?

Ok, firstly this is not about forms this is about consistent layout as a user explores a site.
let me explain:
If we imagine a (non-ajax) digital camera online store, say someone was on the DSLR section and specified to view the cameras in Gallery mode and order by price. They then click onto the Compact camera's page. It would be in the users interests if the 'views' they selected we're carried over to this new page.
Now, i'd say use a session - am i wrong?
are there performance issues i should be aware of for a few small session vars ( ie view=1 , orderby=price) ?
Speaking of performances, there should not be much problems with either solutions.
Some things that have to be considered are :
With GET, if an URL gets copy-pasted (in a email or MSN), the other who will receive the URL will have the same GET parameters
is that a good thing, or not ?
On the other hand, session will not be shared, if an URL is copy-pasted
which means the first guy will say to the other "key, look at this", and the second guy will not see the same page ;; same thing with bookmarking, should I add.
GET is specific to each URL
While SESSION is shared accross all tabs of the user
Which means browsing with several tabs at the same time can cause troubles, when using Session, if you don't take care of that
I'd say use both. Store it in the session, but also put it in the get parameters for the page.
Why? This way the user is able to carry his options from page to page, but they are also in the URL so if he sends search results to his friend, his friend sees them the exact same way he did.
No, the session's performance will not degrade by putting those small variables in there. Unless you're storing monolithic arrays in your session, the vast majority of the time loading a session will be reading it from its storage medium (file, database, memcache, etc).
You should use GET in your case.
There is one simple rule in the web development: each page with different content must have it's own address. So, customer can save any page into favorites, send it to a frend. It's pain in the bottom then someone sends you a link to a particular page saying "Look!" but site uses frames and you land at the front page and dunno where to look.
You can save user's preferences into his profile/cookie (not session), but it should be reflected in the address bar as well.
Sessions being used for completely different purpose, shopping cart is an example.
It's a subjective question, it would work either way.
Personally I would go with sessions as it doesn't interfere with the URL so people can bookmark the url if they wanted.
However the argument for that would be if they bookmarked it they might see different things if it was done using $_SESSION.

Categories