Opens in a new tab

Onclick Event - Detailed instructions

Updated on 3 min read

In the following example, we implement conversion tracking for a click on a menu item on a WordPress site.

In our example, we want to track all clicks that are made on the "New here?" menu item.

Step 1 – Creating the Conversion in Trackboxx

To do this, open the website overview via Settings (the cogwheel) and select “Goals” for the relevant website.

Here we add a new goal and give it a suitable name. In our case: Click “New here?” – Main menu.

Once the goal is created, we now have the corresponding code snippet. The most important element here is actually the ID in the script.

Our article “How to use conversion tracking” explains several ways to integrate the conversion script. Here we explain the “OneClick” event: clicks on a menu item are tracked using its unique CSS class.

In the case of WordPress, for example, it is not easy to enter our conversion code in the menu, but we can label the menu item "unique" and thus track it without any problems.

Step 2 – Adding a CSS Class to the Menu Item

First, go to the WordPress admin panel and navigate to Appearance – Menus to access your corresponding menu.

To be able to add a CSS class here, you may need to activate this option first. To do this, click on the "Customise view" option at the top and activate the "CSS classes" checkbox.

After enabling this option, the menu item has a CSS class field. In our example, enter “neu-hier” and save the changes.

Step 3 – Adjusting and Implementing the Conversion Code

Now we adapt our conversion code. Integration via the OneClick event uses a fixed format. You can also find these formats in our conversion tracking overview.

We need to insert our unique CSS class into the code, and our final script will look like this:

<script type='text/javascript'>
document.querySelector('.neu-hier').addEventListener('click', () => {
   trackboxx('trackGoal', 'WNUXTLZ2GT');
});
</script>Code language: HTML, XML (xml)

Additional information! In general, you have the option to use either a CSS ID or a CSS class to uniquely identify a menu item or button. In WordPress menus, only the CSS class can be used, which is why in this script we use document.querySelector instead of document.getElementById.

If you assigned a CSS ID, the code would look like this:

<script>
document.getElementById('neu-hier').addEventListener('click', () => {
   trackboxx('trackGoal', 'WNUXTLZ2GT');
});
</script>Code language: HTML, XML (xml)

Important: Your code will have a different `trackgoal: ID`, and depending on the CSS class you used, the class name in the code must also reflect the correct name.

Step 4 – Embedding the Conversion Code into Your Website

In our case, we want to track clicks on the menu item across the entire website. We therefore insert the code into the website footer.

If you want to track a click on a specific page only, it’s sufficient to embed the conversion script on that page alone.

Embedding the code in the footer in WordPress can be done using various methods, such as:

  • Embedding via a template + page builder:
  • Integration via plugin e.g. "Header Footer Script"
  • Embedding via Theme Settings or Page Builder Settings:
  • Embedding via Child Theme and functions.php:

In our case, we use Elementor for the website and therefore use the "Custom Code" option.

Set the location to </body> End.

We save everything, and the integration of the conversion tracking is now successfully completed. The clicks on the button should now be displayed in the goals section of the Trackboxx dashboard.

Software Made in Germany – German Association of IT SMEs (BITMi)