Goal tracking in JS tracking code


#1

Hey all. My case is this, i have some links i would like to track on which if I am correct Matomo does automatic, BUT i would like to setup a goal for these link, so when a user clicks on the link i get 1 value in my goal. My question is:

to make sure Matomo tracks my goal without using the maunal method am I allowed to this?

<!-- Matomo -->
<script type="text/javascript">
  var _paq = _paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  _paq.push(['trackGoal', 1]); // The goal that tracks the link
  (function() {
    var u="URL";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', 'number']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

Or should it be elsewhere ?
Hope it is understandable slight_smile:


(Roddy A. Stegemann) #2

If I remember correctly you can push to the _paq array anywhere in a document where Javascript is implemented, As a good rule of thumb, however, make sure that you push before you push ‘trackPageView’. So, in the provided code write:

_paq.push(['trackGoal', 1]); // The goal that tracks the link
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);

Roddy


#3

Thank you very much I will try that :slight_smile:


(Peterbo) #4

That’s not how it works. You have to attach the goal trigger to the click on a link. You can either set up the goal in the User Interface that it is triggered with the click on an external link (with or without certain conditions), or you can attach an event listener on the page which triggers the goal on an external click manually (e.g. with jQuery)

    $("a.externalLinkClass").click(function () {
        _paq.push(['trackGoal', 1]);
    });

(Daniel Dixon) #5

Its a great code and thanks for sharing this above js tracking code it will be informative for me.

Regards,

TechTIQ Solutions,