0

We are attempting to use the following code to pass order details to google analytics. I read several posts that were very close, but was not able to resolve my issue. The problem is in the definition or use of variable "item". The line towards the bottom: 'name': '<%= item[0] %>', errors saying "item does not exist in current context".

I appreciate any suggestions.
Thanks.

 function TrackOrderLines() {
       var orderLines = "<%= GetOrderLines() %>";
       if (orderLines !='') {
           var items = orderLines.split(',');

    for (var i = 0; i < items.length; i++)
    {
        if (items[i] !='') {
            var item = items[i].split('|');

//This is here just to prove that we are getting the correct value in item.
            document.getElementById("demo").innerHTML = item[0];

        ga('ecommerce:addItem', {
            'id': '<%# salesOrder.OrderType + "-" + salesOrder.OrderNumber.ToString() + "-" + salesOrder.Company %>',                     // Transaction ID. Required.
            'name': '123',    // Product name. Required.
            **'name': '<%= item[0] %>',**
            //'category': 'Party Toys',         // Category or variation.
            //'price': '111',                 // Unit price.
            //'quantity': '123'                   // Quantity.
});
                       }
                 }
          }
   }

1 Answer 1

1

it seems that item is actually a JS variable, but you're trying to print it from C# using <%= %>

Please try this:

ga('ecommerce:addItem', {
            'id': '<%# salesOrder.OrderType + "-" + salesOrder.OrderNumber.ToString() + "-" + salesOrder.Company %>',                     // Transaction ID. Required.

            'name': item[0],
            //'category': 'Party Toys',         // Category or variation.
            //'price': '111',                 // Unit price.
            //'quantity': '123'                   // Quantity.
});
Sign up to request clarification or add additional context in comments.

1 Comment

You're right, thanks for pointing that out. We've tried several variations trying to get this to work and overlooked that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.