Adding OXID Product ID and Price to datalayer for Google Tag Manager

409 Views Asked by At

I would like to add product ID and price to the google tag manager datalayer in OXID Eshop. Is there a simple method of doing this?

I thought I might be able to edit the code suggested here: List of multiple products into php datalayer but I am too unfamiliar with oxid code.

Here is what I tried, for example:

<script>
  dataLayer = [{
    'product': '$oView->getProduct()',
    'price': 'various-price-variables'
  }];
</script>

additionally: $oViewConf->getActArticleId() or oxArticle and other variations. Obviously none of these work. Any help would be much appreciated.

1

There are 1 best solutions below

0
Marat On BEST ANSWER

You can't simply pass server side smarty variables to client side javascript, since smarty is rendered before browser receives html. Smarty tries to output string representation of an oxArticle object there, but as far as i know, oxArticles have no __toString() method.

There are 2 possibilities for you:

  1. you can directly map oxArticle properties to datalayer variables you need, like this: (pay attention to single quotes around smarty tags to match data types of javascript object properties)
var product = {
    'name': '[{$oArticle->oxarticles__oxtitle->value}]',
    'id': '[{$oArticle->oxarticles__oxartnum->value}]',
    'price': [{$oArticle->oxarticles__oxprice->value}]
};
  1. you can json_encode the whole $oArticle object var product = [{$oArticle|json_encode}]; but this might expose some produt data that you usually do not want people to see, like purchase price or actual stock. You only get the information that is also stored in the database, but not the danymically computed stuff like the actual price including current discounts or product url.

also, you are welcome to have a look at my tag manager module: https://github.com/vanilla-thunder/oxid-module-tag-manager