phpwordpressgoogle-analyticsgoogle-analytics-apianalytics.js

how to send purchase request to google analytics via Analytics.js with "ga" command?


Okay lets start I did include small script in my "head" template at the all pages

    <script type="text/javascript">
        (function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,"script","https://www.google-analytics.com/analytics.js","ga");

    ga("create", "' . $this->getTrackId() . '", "auto");

    ga("require", "ec");

    ga("send", "pageview");        
    </script>

and other scripts:

for view product detail

<script type="text/javascript">
    ga("ec:addProduct", {               // Provide product details in a productFieldObject.
        "id":"' . $post_id . '",        // Product ID (string).
        "name":"' . $title . '",        // Product name (string).
        "category":"' . $categories . '"       // Product category (string).
    });

    ga("ec:setAction","click", {       // click action.
        "list":"' . $categories . '"          // Product list (string).
    });
    ga("send", "pageview"); 
    </script> 
    <script type="text/javascript">
    ga("ec:addProduct", {
      "id": "' . $post_id . '",
      "name": "' . $title . '",
      "category": "' . $categories . '"
    });
    ga("ec:setAction", "detail");
    ga("send", "pageview"); 
    </script>

for purchase

<script type="text/javascript">
        ga("ec:addProduct", {               
            "id": "'.$product_id_arr.'",                   
            "name": "'.$name.'", 
            "variant": "'.$variant.'",              
            "price": "'.$subtotal.'",                
            "quantity": "'.$qty.'"                    
            });

        ga("ec:setAction", "purchase", {         
        "id": "'.$trans.'"                       
        });
        ga("send", "pageview");  
        </script>

and other classic analytics.js functions

each code includes ga ("send", "pageview"); line, but because of this, pageview statistics increases by several times and is incorrect. Without this line, data such as "purchase" stop being sent. What do i do?

I need to make the number of page views correct, while other data should also be sent, what are the options?


Solution

  • Since you have "send" pageview for all your pages in the head, what you can do is use "events" for the rest. This is assuming product detail and product purchase are all events happening on the same page.

    Example, instead of sending a pageview for product detail, you can send an event:

      ga('send', 'event', 'product', 'view-detail', 'PRODID000111');
    

    for purchase:

      ga('send', 'event', 'product', 'purchase', 'PRODID000111');
    

    the pattern is

      ga('send', 'event', {EVENT CATEGORY}, {EVENT ACTION}, {EVENT LABEL});