How to add HTML, google analytics, tag manager, third party JS code in Opencart

In this Opencart tutorial, we are showing you how to add HTML in Opencart, similarly how to add google analytics, google tag manager, Adroll, Facebook pixels, MailChimp conversion code, google ads conversion in success page only, and other third-party JavaScript code in the Opencart, likewise our best way to manage the JavaScript code through google tag manager and test and preview in the google tag manager.

How to add HTML in the Opencart?

Opencart has an HTML content module in which you can add the custom HTML in the Opencart and show in any layout you want. Go to admin >> Extensions >> Extensions >> Choose the extension type “Modules”, then look for HTML content and then install it. As HTML content is a multi-instance Opencart module, you can install as many different modules as you want.

See more: difference between single instance and multi-instance Opencart module

HTML module content

Once you install, click the add module button of the HTML content and you will see a form like below:

add HTML code saving in Opencart

Click the </> icon then paste the HTML that you have and again click the icon </> and then only the HTML will be saved, else HTML code will not be saved. So be sure to click </> and see WYSIWYG editor and then only click the Save button.

Now, go to admin >> Design >> Layouts and edit where you want to show the module and then select the positions where to show. Here is a video which shows the layouts and position details of Opencart 3

How to add google analytics in the Opencart?

Go to https://google.com/analytics, then log in, choose your accounts and select the “All Web Site Data”, then click in the left menu (1) “Admin”, then to the (2) Tracking Code and then (3) copy the code.

Google analytics code

Once you copy the code, go to the Opencart admin >> Extensions >> Extensions >> Choose the extension type “Analytics”. Click the install button.

Google Analytics Opencart module

After install click the edit button and you will see the form like below where you can paste the javascript analytics code.

Google analytics code JavaScript

Similarly, you can add any third-party JavaScript code here like, the google tag manager, Facebook pixels, MailChimp conversion code, etc.

How to add conversion code on the success page of Opencart?

Conversion codes are mostly added on the success page. Here are the steps to add conversions the Opencart way, it may long way but it is Opencart way taking layouts into consideration:

  1. Go to admin >> Extensions >> Extensions >> Choose the extension type “Module” and install the Custom HTML module >> add new >> Enter module name >> Click the </> icon on description >> insert the JavaScript code >> Click the </> icon again >> select Enabled on status >> click Save.
    conversion-code-ads-success-page-opencart
  2. Now, go to admin >> Design >> Layouts
  3. Edit the checkout layout
  4. Change the module name to “Checkout Cart”
  5. In the route field change “checkout/%” to “checkout/cart”
  6. Now, click add a new layout, then enter module name is “Checkout Checkout” and in the route field click “add new” button and add the route as “checkout/checkout” and then click Save.
    Checkout Layout
  7. Similarly, click add a new layout, then enter the module name as “Checkout Success” and in the route field click “add new” button, enter “checkout/success”, then click the add button on the Content Bottom position and select the Conversion code HTML module, then click Save button.
    checkout-success-layout

  8. With the above steps, the conversions code is shown only on the success page.
    conversion-code-at-success-page-only

In this way, you can add conversion code on the success page only. We made three different layouts because checkout/% took preferences so we cannot create only checkout/success only. Thus we need to create for all of the routes that checkout contains, thus there are three different layouts.

If you are a developer and using the default theme then you can add the code directly into the code from theme editor. Go to admin >> Design >> Theme Editor >> Choose your store mostly “Default” >> Choose the template “Common” >>then click “success.twig” and add JavaScript code above {{footer}}.

add code in success twig of Opencart

How to add custom JavaScript code and custom CSS code in the Opencart Journal theme?

Go to admin >> Journal >> System >> Settings >> Edit your store >> Then Custom Code >> you can add css and Javascript here.

Journal theme custom CSS JS

Our best way to manage third-party JavaScript code is by using Google Tag manager

We use Google Tag Manager to manage third-party JavaScript code and we think this is the best way because all of our external JavaScript will be on Google Tag manager and we can control a lot of things in google tag manager. Go to http://tagmanager.google.com, sign in, and create an account.

google-tag-manager-account

Enter the Account Name, select Country, Container Name, and select Web for a website, then click create button. You will get code to add in your website:

gtm-code-opencart

Copy the JS code and go to Opencart admin >> Extensions >> Extensions >> Choose extension type “Analytics”, then install the Google Analytics and edit and you can add the Google tag manager code at the “Google Analytics Code”, enabled it and click save. Once these are done, the google tag manager is installed in Opencart.

Now in the Google tag manager admin section, click the Tags, then click New, click Triggering, click “All Pages”, after that click the Tag configuration, choose the custom HTML, where you can add any third party JavaScripts.

Here let’s take an example of MailChimp. Login to the MailChimp, click on the profile, in the dropdown click Connect, then integrations,

mailchimp custom html tracking

Then custom website, enter the website URL, and click “Get Code”.

custom website mailchimp tracking

You will get like below:

mailchimp tracking code

Copy that code and paste that code in the google tag manager Custom HTML like below:

google tag manager mailchimp

Click Save. Then submit.

Save Google Tag Manager

Enter the version details:

GTM mailchimp Opencart

It saved the tag and show details like below:

Version saved in GTM opencart

Now your tag is shown in your Opencart store. Likewise, you can add as many third-party JavaScript codes as you want, similarly, you can add code on different pages with the help of triggers.

In this way, you can add the custom Javascript, custom HTML module, CSS, google analytics, Mailchimp tracking code, google tag manager, etc in the Opencart. Please post your questions or comments or errors so that we can help you. You can follow us at our twitter account @rupaknpl. Subscribe to our YouTube channel for Opencart tutorials, and click to see all Opencart tutorials.

Previous articleCreating a Custom Page in OpenCart 3 – categories listing page
Next articleOpencart code snippets VSCode extensions

LEAVE A REPLY

Please enter your comment!
Please enter your name here