Knowledge Base      


Can I record a conversion or goal using JavaScript?


You sure can! Just use the log() function available for both Tracking Links and Campaigns ...




The first step is to make sure you have either Campaigns Click or Conversion Tracking code on your page.

For example, here’s the Click Tracking Code for your account:

<script>
    var clickmagick_cmc = {
        uid: '[USERID]',
        hid: '[HID]',
    }
</script>
<script src='//cdn.clkmc.com/cmc.js'></script>


With that code on the landing page, you can call the clickmagick_cmc.log() function from any JavaScript code.

The clickmagick_cmc.log() function has two forms, one for Actions and Engagements, and another for Sales so that you can pass the amount of the sale:

clickmagick_cmc.log('a', ref, project);              // Or 'e'
clickmagick_cmc.log('s', ref, amount, project);


For these calls, only the first goal argument is required. All the others are optional, or you can pass the empty string ''. For example, it would be typical to record an engagement goal in an onclick handler like this:

onclick="clickmagick_cmc.log('e')"

You could record a sales goal (with no reference value) like this:

onclick="clickmagick_cmc.log('s', '', 10.95)"

With this setup, you can for example add an onclick handler to the link or button that redirects to an affiliate offer. When the button is clicked, the onclick handler is fired, recording the goal, then your button redirects as normal.

A complete <a> link might look like this:

<a href="https://conference-reg.com/" onclick="clickmagick_cmc.log('a', 'register')">Register Now</a>

And, of course, you are not restricted to calling clickmagick_cmc.log() in onclick handlers. You can call it from any JavaScript code on your page, including click handlers using jQuery.


The first step is to make sure you’re loading ClickMagick’s cmtools.js library on your page.

For example, here’s the cmtools.js code with your User ID:

<script>
    var cmtools = {
        uid: '[USERID]',
    }
</script>
<script src='//cdn.clickmagick.com/misc/js/cmtools.js'></script>


With that code on the page, you can call the cmtools.log() function anywhere in your JavaScript code.

The cmtools.log() function has two forms, one for Actions and Engagements, and another for Sales so that you can pass the amount of the sale:

cmtools.log('a', ref);            // Or 'e'
cmtools.log('s', ref, amount);


For these calls, only the first goal argument is required—the others are optional or you can pass the empty string ''. For example, it would be typical to record an engagement goal in an onclick handler like this:

onclick="cmtools.log('e')"

You could record a sales goal (with no reference value) like this:

onclick="cmtools.log('s', '', 10.95)"

With this setup, you can for example add an onclick handler to the link or button that redirects to an affiliate offer. When the button is clicked, the onclick handler is fired, recording the goal, then your button redirects as normal.

A complete <a> link might look like this:

<a href="https://conference-reg.com/" onclick="cmtools.log('a', 'register')">Register Now</a>

And, of course, you are not restricted to calling cmtools.log() in onclick handlers. You can call it from any JavaScript code on your page, including click handlers using jQuery.

 
Note: Adding onclick handlers is not supported by all page builders—the onclick handler will often just be stripped out before your page is displayed. For example, adding onclick handlers with LeadPages works fine, but ClickFunnels will silently remove them.

If you set up your click handler programmatically using JavaScript or jQuery, that works great in all cases—simply put the code in the header or footer section and set up the handler once the page has fully loaded.

If you can’t get JavaScript pixels to work with your particular setup, then you can use a hidden intermediate page to record your conversion. You can read how here:

How can I set up an intermediate page to fire a conversion?


Article 581 Last updated: 04/23/2021 6:59:53 AM
https://www.clickmagick.com/kb/?article=581