What is the most lightweight method of tracking all outgoing links generated in a view. By lightweight I mean that I don't want to have to load a module such as statistics that will potentially slow down my site due to it going above and beyond my needs.

Basically I would need to record the data into a database using the following structure:

product_id | uid | timestamp

The product id is associated with each link and is a separate field. The purpose would be to create sets of clicked links for each user, product, etc. This could in turn be used as a dataset for subsequent user/item analysis. I would imagine I would need to use some sort of callback function.

Can anyone provide an example or any hints?

  • Timestamp of what, exactly? And why do you have to log Views results? Views queries I can understand, but results? And please, try to rewrite this in a non-opinion-based way. Currently it heavily depends on your definition of "lightweight". – Mołot Apr 24 '14 at 19:41
  • By timestamp I mean the time that the link was clicked. It doesn't have to be exact, so for that I can simply use the database insert time. The need for the product id - uid relationships is for developing recommendation engines. – John Apr 24 '14 at 20:26
  • Oh, so you want to count clicked links, not generated ones. That explains a bit. Could you please update your question? – Mołot Apr 24 '14 at 20:31
  • are you opposed to using google analytics event tracking? It is ideally suited for capturing the information you describe, and is done via javascript. – jdu Apr 24 '14 at 20:57
  • I would prefer a different method. – John Apr 25 '14 at 4:02

You could use jQuery to attach a click event to each link that triggers an ajax request to record the click.

The problem with this is that the page may redirect before the ajax request has had a chance to fire so you will have to prevent the default action of the click, and then perform an ajax request with async=false, and then provide a call back function to redirect to the links destination.

Not exactly lightweight...

Another option is to define an outbound link function in PHP that will accept the final destination as a argument. So the link would look like /outbound?dest=http%3A%2F%2Fwww.google.com.

Then in that function you can record the click, and then redirect them on to the destination passed in...

  • The second option sounds like a good option. Is there any reason why the second one would not be ideal? – John Apr 25 '14 at 14:20
  • I've actually used the second option before and it worked fine. I guess I feel that because Google analytics is designed specifically for doing this stuff that it will work better than my solution, so for me to call my solution "ideal" I have to be more intelligent than all the programming bods at Google, so I'm reluctant to make that claim... – Felix Eve Apr 25 '14 at 14:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.