ZK Zhao
ZK Zhao

Reputation: 21523

Rails: count the click number of a link?

I want to count the number of user visiting a link in my website(http://productchaseapp.herokuapp.com/). Specificly, I want to know the click number of the product.url (user visiting the external url of a product). This is how the link works:

link_to product.name, product.url, target: '_blank'

The problem is that, by doing this, there is no way that I can actually update the clickCount of the product, after all, after user clicks the link, they are outside of my website.

I think one solution, is to change the the link to

link_to product.name, product_path(product), target: '_blank'

And in ProductsController#Show, update the attribute and redirect the user outside of the website to product.url.

I'm not sure if this is a good solution, and what are other good ways of doing this.

Upvotes: 3

Views: 957

Answers (2)

fylooi
fylooi

Reputation: 3870

One way to handle it is to override the click event of the link in question to fire a PUT back to an endpoint in your app which tracks link clicks, then redirect to the external URL.

Upvotes: 0

user626511
user626511

Reputation:

Try https://ankane.github.io/ahoy/

Ahoy provides a solid foundation to track visits and events in Ruby, JavaScript, and native apps.

Works with any data store so you can easily scale.

For example, you can do:

ahoy.trackClicks();

Upvotes: 3

Related Questions