How to add Ecommerce GTM tracking script to page template

Fat Media Ltd asked on June 8, 2020 17:07

Hi. I am trying to follow the instructions for adding Ecommerce GTM tracking code for product detail views as described on this page - https://docs.kentico.com/k11/e-commerce-features/customizing-and-developing-your-store/integrating-google-analytics-enhanced-ecommerce#IntegratingGoogleAnalyticsEnhancedEcommerce-Viewsofproductdetails

My product detail page is just a template (.aspx type), not a transformation, but when I try to add the script to the template I get a syntax error saying GetGtmProductJson() doesn't exist in the current context. I am adding it to the template and not via the Javascript web part because I need to pass the SKUID to the method if the template is for a product, and hide the script if not.

Can anyone advise how you can add this GTM tracking script to a page template, i.e. is there an import I need to allow the method call? Thanks.

Recent Answers


David te Kloese answered on June 8, 2020 17:53

method GetGtmProductJson() is part of "CMS.DocumentEngine.Web.UI" but will call

EcommerceTransformationFunctions.GetGtmProductJson(SKU, additionalData, purpose)

Since you want to run it from a template I recommend you either create a Web Part that pulls the logic to code or create a User Control that does it (which you can embed directly in your template. I favor the Web Part as it's more clear when you view the Design tab it's on.

0 votesVote for this answer Mark as a Correct answer

Nikhila Reddy Pocha answered on June 8, 2020 20:13

You can simply use the javascript webpart and place your gtm script into the respective page template.

2 votesVote for this answer Mark as a Correct answer

Al Burns answered on July 27, 2020 16:56

Hi. I have added the Javascript web part to my template and added the following script snippet:

document.addEventListener("DOMContentLoaded", function (event) {
  dataLayer.push({
    "ecommerce": {
      "detail" : {
        "products": [<%# GetGtmProductJson() %>]
      }
    }
  });
});

However, when I view the page I get a console error: SyntaxError unexpected token '<'. Does anyone know what the issue might be? Thanks.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.