Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sends only half Ecommerce transactions #4

Open
poma opened this issue Oct 27, 2014 · 7 comments
Open

Sends only half Ecommerce transactions #4

poma opened this issue Oct 27, 2014 · 7 comments

Comments

@poma
Copy link

poma commented Oct 27, 2014

Doesn't seem to work properly and sends only half of data. In javascript everything works fine. I use it like this:

$t = new \Std\GATracker('UA-12345678-1', null, $tr->userId);
$t->send('transaction', array(
  'transactionId' => $tr->id,
  'transactionRevenue' => $tr->price
));

foreach($items as $item) {
    $t->send('item', array(
      'transactionId' => $tr->id,
      'itemName' => $item['name'],
      'itemCode' => $item['id'],
      'itemCategory' => $item['category'],
      'itemPrice' => $item['price'],
      'itemQuantity' => 1
    ));
}

tcpdump shows that all requests are actually sent to analytics server so I don't know what is happening. Any ideas?

@samba
Copy link
Contributor

samba commented Oct 28, 2014

@poma would you mind sharing the request log? Setting the $debug option on the Tracker constructor and running it via php -f should print the output via CURL's native features.

If you're seeing everything transmitted, I'm wondering if the bug is related to GA throttling the hits or somehow misinterpreting them. If you can provide the request log, I'd be interested to review and see if I can replicate the issue.

Thanks!

@poma
Copy link
Author

poma commented Oct 29, 2014

Here is a example code that sends data through backend and mirrors it with JavaSript. It also logs all sent transactions. All transactions are sent through JS successfully but only part of them are successfully sent with PHP. Also user id tracking doesn't work. If you go to transactions tab in analytics and select 'Source' as a secondary dimension all transactions reported by backend will show up as '(direct)' although they are reported with the same userId as frontend ones.

update: I ran this test from localhost so that if any problems are caused by throttling they will affect both JS and PHP side.

<?

require('universal-analytics.php');
$tr_id = 'test-'.rand(1, 1000);
$property = 'UA-12345678-1';
$domain = 'example.com';

// First report mirror transaction with JavaScript (with 'js-' prefix)
?>
<!doctype html>
<html lang="ru">
<head>
<script>
    (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', '<? echo $property ?>', '<? echo $domain ?>', {'userId': '1'});
    ga('send', 'pageview');
    ga('require', 'ecommerce');
    ga('ecommerce:addTransaction', {
        'id': 'js-<? echo $tr_id ?>',
        'revenue': '100'
    });

    ga('ecommerce:addItem', {
        'id': 'js-<? echo $tr_id ?>',
        'name': 'test product 1',
        'sku': 'tst-1',
        'category': 'test-cat',
        'price': '60',
        'quantity': '1'
    });

    ga('ecommerce:addItem', {
        'id': 'js-<? echo $tr_id ?>',
        'name': 'test product 2',
        'sku': 'tst-2',
        'category': 'test-cat',
        'price': '40',
        'quantity': '1'
    });

    ga('ecommerce:send');
</script>
</head>
<body>
<pre>
<?
    // Then report it from backend
    $t = new Tracker($property, null, '1', true);
    $t->send('transaction', array(
      'transactionId' => $tr_id,
      'transactionRevenue' => 100
    ));

    $t->send('item', array(
      'transactionId' => $tr_id,
      'itemName' => 'test product 1',
      'itemCode' => 'tst-1',
      'itemCategory' => 'test-cat',
      'itemPrice' => 60,
      'itemQuantity' => 1
    ));

    $t->send('item', array(
      'transactionId' => $tr_id,
      'itemName' => 'test product 2',
      'itemCode' => 'tst-2',
      'itemCategory' => 'test-cat',
      'itemPrice' => 40,
      'itemQuantity' => 1
    ));
    file_put_contents('transactions.log', $tr_id."\n", FILE_APPEND);
    echo 'Reported transaction '.$tr_id;
?>
</pre>
</body>
</html>

@samba
Copy link
Contributor

samba commented Dec 2, 2014

@poma there's also Issue #5 - the buffering of requests and flush behavior - recently noted as needing better documentation. Is it possible that in your environment the hits are being buffered, but the Tracker isn't being __destruct()ed (properly deleted)?

@samba
Copy link
Contributor

samba commented Dec 2, 2014

@poma can you also clarify the effect you're seeing?

Are you missing entire transactions, or are the transactions partially reported (i.e. some items and not others)?

@samba
Copy link
Contributor

samba commented Dec 2, 2014

@poma FYI I've run 12 tests this morning and have been unable to reproduce any issues with the test code you provided.

I've tried some against master, as well as with a patch (soon to be in testing branch) to provide the a Tracker->flush() method. Both are reporting accurately in my tests.

@aboritskiy
Copy link

According to this and this:

  1. _trackPageview need to be added before
  2. _trackTrans need to be added after

I looked through the code of universal analytics and haven't spotted that those are added automatically for ecommerce tracking.

I'm also not usre if I should use commands like
_trackPageview, _addTrans, _addItem and _trackTrans
or pageview, transaction, item and <smth-else-for-track-trans>

UPDATE:
this worked for me:

    $tracker = new Tracker('UA-xxxxxxxx-2', null, null, true);

    $transactionId = uniqid();

    $tracker->send('pageview', array(
      'page' => '/test-page?' . $transactionId,
      'title' => 'my fancy virtual test page'
    ));

    // Send a transaction
    $tracker->send('transaction', array(
        'transactionId' => $transactionId,
        'transactionAffiliation' => 'Test affilate',
        'transactionRevenue' => 149, // not including tax or shipping
        'transactionShipping' => 10,
        'transactionTax' => 10
    ));

    // Send an item record related to the preceding transaction
    $tracker->send('item', array(
        'transactionId' => $transactionId,
        'itemName' => 'Rice Mixed',
        'itemCode' => 'test-rice-mixed',
        'itemCategory' => 'Default',
        'itemPrice' => 149,
        'itemQuantity' => 1
    ));

    // Send an item record related to the preceding transaction
    $tracker->send('trackTransaction', array());

@samba
Copy link
Contributor

samba commented May 29, 2015

@aboritskiy the _trackPageview and _trackTrans calls are related to GA Classic ga.js and have no bearing in this PHP library.

Could you re-test, after removing your last $tracker->send('trackTransaction'...) call? That one should not be necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants