Knowledge Base      


How do I record a conversion or goal using JavaScript?


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

If you have a Single Page Application, install the 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',
        vid_info: 'on',
    }
</script>
<script src='//[CODE-DOMAIN]/[CODE-FILENAME].js'></script>


Be sure to change NameOfYourProject between the 's to be your project’s name or use the Website Code Tool to get your actual tracking code with the correct Project name.

With that code on the page, you can then call the clickmagick_cmc.log() function to record conversions in JavaScript. Simply pass it an unnamed object containing one or more cmc parameters, which you can learn about here:

What are the optional “cmc” Parameters?

Not all cmc parameters are supported by the log() function as some of them just aren’t applicable. Here are the parameters the log() function supports:

cmc_goal
cmc_ref
cmc_amt
cmc_project
cmc_currency
cmc_unique
cmc_upsell
cmc_recurring
cmc_cogs
cmc_email
cmc_firstname
cmc_lastname
cmc_fullname
cmc_phone


So for example, to record a sale with a ref value and an amount you would pass cmc_goal, cmc_ref and cmc_amt to the log() function like this:

clickmagick_cmc.log({
  cmc_goal: 's',
  cmc_ref: 'offer1',
  cmc_amt: 97.00
});


If you for some reason wanted to record “duplicate” conversions you’d also pass in cmc_unique with a value of 0 like this:

clickmagick_cmc.log({
  cmc_goal: 's',
  cmc_ref: 'offer1',
  cmc_amt: 97.00,
  cmc_unique: 0
});


And if you wanted to also record the customer’s name and email you’d pass that in like this:

clickmagick_cmc.log({
  cmc_goal: 's',
  cmc_ref: 'offer1',
  cmc_amt: 97.00,
  cmc_unique: 0,
  cmc_firstname: 'Joe',
  cmc_lastname: 'Blow',
  cmc_email: 'joeblow@gmail.com'
});


Using the log() function you can for example add an onclick handler to any link or button. When the button is clicked, the onclick handler is fired which then records the conversion.

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.


 
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 1010 Last updated: 10/16/2025 10:14:47 AM
https://www.clickmagick.com/kb/?article=1010