Knowledge Base      


How do I record a conversion or goal using JavaScript?


It’s easy! Just use the log() function available for both Campaigns and Tracking Links ...




The first step is to make sure you have either Campaigns Click or Conversion Tracking code on your page. (If you’re creating a single page application, install the Campaigns Click Tracking code—not the Conversions Tracking code—since you’ll want the click to be recorded when the page is first loaded.)

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

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


Be sure to change NameOfYourProject between the 's to be your project’s name.

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.


Article 581 Last updated: 03/22/2024 10:30:11 AM
https://www.clickmagick.com/kb/?article=581