简体   繁体   中英

Google Analytics : remove/edit a transaction from back-office

I have a little problem with Analytics.

To correct some mistakes that may appear in transactions (user not coming back on the site after payment for exemple) I tried to implement a fixing page in the back office.

With the PHP API, i retrieve all of the transactions and I display it them into a table with an input and a button to execute removing script.

But it doesn't work. The code is executed correctly, and Analytics Chrome extension sees no error, but there is no modification in the stats.

Here is a sample of my code.

//Top of my page

<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','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-10XX3X1-1', 'xxxxxxx.com');
    ga('require', 'ecommerce', 'ecommerce.js');
</script>

/*
Display of Transactions
*/

<script type="text/javascript">
        function modifiyTransaction(e){
            var valueT=document.getElementById(e.name).value.toString();
            console.log(typeof valueT+" : "+ valueT);
            console.log(typeof e.name+" : "+ e.name);

                    //Fixing transactions

                    ga('ecommerce:addTransaction', {
                        'id': e.name, // order ID - required
                        'affiliation': 'Rental', // affiliation or store name
                        'revenue': valueT // total - required
                    });
                   ga('ecommerce:send');
                   $("#sendingEvent").show();
                   setTimeout(function(){
                        document.getElementById(e.name).value="Done";
                        $("#sendingEvent").hide();
                    }, 1000);
        }       
</script>

The value and the id I get are correct and both strings.

Does someone have a solution ? :)

Edit :

If this is not possible this way, can it be done directly via the PHP API ? I did not see something related to this in the doc.

Ok so, using the Measurement Protocol with AJAX worked.

Here is a sample of my code if someone is struggling with the same problem.

$.ajax({
                type: "POST",
                url: 'https://ssl.google-analytics.com/collect',
                data: { v: "1", tid: "UA-XXXXXXXX-1", cid: clientId, 
                       t: "transaction", ti: transactionId, 
                       ta: "Rental", tr: valueToChange },
                success: function(){
                    $("#sendingEvent").hide();
                    document.getElementById(e.name).value="Done";
                },
                error: function(){
                    $("#sendingEvent").hide();
                    document.getElementById(e.name).value="Error!";
                }
            });

Finally super easy.

For more information : https://developers.google.com/analytics/devguides/collection/protocol/v1/?hl=fr + the developer guide and the reference guide linked in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM