How to catch click events on a link in order to fire a pixel


(Alon Levi) #1

Hi @team, Is there any way to catch click event on a link and fire a pixel to google adwords.
I want to know if user click on an amazon link on my site.

I saw this topic

Anything similar to this code can be done with a link? (html a href)

Thanks


(Joe Buhlig) #2

You should be able to catch click events with jquery via a customization. Put this in the </head> section:

<script type="text/discourse-plugin" version="0.2">
    Discourse.PageTracker.current().on('change', function () {
        $(".topic-list-item .title").on("click", function(){
            console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        })
    })
</script>

This will log the string of x`s every time you click on the title of a topic in a list of topics. You could alter this to do whatever you please for every link on the site.


(Alon Levi) #3

Thanks, it worked, I end up using the api.onPageChange and also add setTimeout since the page changed and not all page was render to set the click binding.


(Robin Ward) #4

I should warn you that setTimeout is unreliable for knowing when things are rendered. It might work in your testing but there could be times when the timeout fires before things have been rendered.