javascriptgoogle-analyticsenhanced-ecommerce

Will this count as two pageviews in GA, and do I need the second pageview?


I'm having a hard time finding clear documentation on how to handle this situation and what best practise is.

Question:
How off is my implementation and what steps should I take to fix it? Will it count as two pageviews? Do I need the second pageview?

Implementation
I my head

<script>
  // analytics.js

  ga('create', 'UA-XXXXXXXX-XX', 'auto');
  ga('require', 'ec');
  ga('set', 'dimension1', 'somevalue')
  ga('set', 'dimension2', 'somevalue')
  ga('send', 'pageview');
</script>

on pageload in checkout

for (var i = 0; i < cart.products.length; i++) {
  var product = cart.products[i];

  const gaProductObject = {
    'id': product.id,
    'name': product.name,
    'category': product.category
    'price': product.price,
    'quantity': product.quantity,
    'brand': product.brand
  }
  
  ga('ec:addProduct', gaProductObject)
}

if (this.user.isAuthenticated) {
  ga('ec:setAction', 'checkout', {
    'step': 2,
    'option': null
  })
} else {
  ga('ec:setAction', 'checkout', {
    'step': 1,
    'option': null
  })
}

ga('set', 'dimension1', this.user.market)
ga('set', 'dimension2', this.user.language)
ga('send', 'event', 'checkout', 'view')
ga('send', 'pageview')


Using: GA and Enhanced Ecommerce.
Site: hybrid .NET and Vue. No Virtual Views required page do pageloads on each navigation.

Thankful for feedback.


Solution

  • The code in head will send a pageview and the one in body will send another. So if the page is the same you will send 2 pageviews.

    If you don't want to send a second pageview (and if the page is the same you shouldn't send it) you can send an event to Google Analytics instead of a pageview. This way the ecommerce information is sent anyway and the pages count is more reliable.