theiconic / php-ga-measurement-protocol

Send data to Google Analytics from the server using PHP. Implements GA measurement protocol.
MIT License
656 stars 135 forks source link

E-commerce data not flowing. #59

Open Marduuk opened 6 years ago

Marduuk commented 6 years ago

Hello!

I'm trying to implement php-ga in my web application, I wouldn't normally write here but I'm in really big dead end. In short, my e-commerce data ain't flowing to the analytics site (I get a notification). Of course there can be plenty of reasons why it's that way , but, I made same implementation on testing environment and everything there works fine. There is already javascript implementation of analytics on this app, also I'm using clientID obtained on google developers API console, while I should be using client ID from ga cookie? Could someone give me some tips or possible cause of this problem?

This is what I'm getting from hit validator:

--- DEBUG: '{"hitParsingResult":[{"valid":true,"parserMessage":[],"hit":"\\/debug\\/collect?v=1&tid=UA-4XXXXXXX-1&cid=149XXXXXX-e6aXXXXXXXXXXXXXXXXa.apps.googleusercontent.com&ti=DSHMEGZQ65CR34K&tr=4001.00&cu=PLN&t=transaction"}],"parserMessage":[{"messageType":"INFO","description":"Found 1 hit in the request."}]}

--- DEBUG: '{"hitParsingResult":[{"valid":true,"parserMessage":[],"hit":"\\/debug\\/collect?v=1&tid=UA-4XXXXXXXX-1&cid=149XXXXXXX-e6XXXXXXXXXXXXX.apps.googleusercontent.com&ti=DSHMEGZQ65CR34K&tr=4001.00&cu=PLN&t=item&in=paymentReceived&ic=885&iv=transfer&ip=4001.00&iq=1"}],"parserMessage":[{"messageType":"INFO","description":"Found 1 hit in the request."}]} 

And the implementation itself looks like that:

        $analytics = new Analytics(true);
        $amount = $this->currencyConversionToPLN($eventValue, $currency);
         $analytics
            ->setProtocolVersion('1')
            ->setTrackingId($trackingId)
            ->setClientId($clientId)
            ->setTransactionId($transactionID)
            ->setRevenue($amount)
            ->setAsyncRequest(true)
            ->setCurrencyCode('PLN')
            ->sendTransaction();

        $analytics
            ->setTransactionId($transactionID)
            ->setItemName($eventName)
            ->setItemCode($userSKU)
            ->setItemCategory($eventCategory)
            ->setItemPrice($amount)
            ->setAsyncRequest(true)
            ->setCurrencyCode('PLN')
            ->setItemQuantity(1)
            ->sendItem();

I would really love if someone could give me any tip or clue why it might not be working.

JanGalek commented 6 years ago

I'm using Enhanced E-commerce, try this (change to your data)

$analytics = new Analytics(true);
$amount = $this->currencyConversionToPLN($eventValue, $currency);
$analytics
       ->setProtocolVersion('1')
       ->setTrackingId($trackingId)
       ->setClientId($clientId)
       ->setTransactionId($transactionID)
       ->setRevenue($amount)
       ->setAsyncRequest(true)
       ->setCurrencyCode('PLN');

$analytics->addProduct([
    'sku' => $product->sku,
    'name' => $product->name,
    'category' => $product->category->name,
    'price' => $product->price * $product->vat,
    'quantity' => $product->amount,
    'position' => 1,
]);

// Don't forget to set the product action, in this case to PURCHASE
$analytics->setProductActionToPurchase();

// Finally, you must send a hit, in this case we send an Event
$analytics->setEventCategory('Checkout')
    ->setEventAction('Purchase')
    ->sendEvent();
Marduuk commented 6 years ago

Ah thank you will try to do that this way :)

Marduuk commented 6 years ago

Ayee! It's working thanks! By the way, do you maybe have custom events implementation? :)

JanGalek commented 6 years ago

np, no i havent custom events, I have this https://github.com/theiconic/php-ga-measurement-protocol/issues/59#issuecomment-383075119 with foreach for addProduct

I like it works ;)

Marduuk commented 6 years ago

Sweet, thanks again, I've been fighting with that for quite some time :)

JanGalek commented 6 years ago

np, I gladly helped ;)

KOEI5113 commented 6 years ago

Hi, I got some similar problem here.

I trigger my event and see the event into the real-time dashboard but somehow their no any data into Ecommerce Overview.

Here is my code:

    $TrackingId     = 'SOMETHING';
    $Affiliation    = 'SOMETHING_TOO';

    $analytics = new \TheIconic\Tracking\GoogleAnalytics\Analytics(true);
    $analytics->setProtocolVersion('1')->setTrackingId($TrackingId)->setUserId('2-00000043')->setTransactionId('123456')
              ->setAffiliation($Affiliation)->setTax(6300*0.05)->setShipping(0)->setRevenue(6300)->setAsyncRequest(true)->setCurrencyCode('TWD');
    $analytics->addProduct(['sku' => '12345','name' => 'testData','category' => 'testData/subTest','price' => 6300,'quantity' => 1,'position' => 1,]);

    $analytics->setProductActionToPurchase();
    $analytics->setEventCategory('Purchase')->setEventAction('debug')->sendEvent();

    $response = $analytics->setDebug(true)->sendPageview();
    $debugResponse = $response->getDebugResponse();
    print_r($debugResponse);

The result of print_r($debugResponse) is an empty array which is really confusing me. Why will I get an empty array if the event does get in the real-time dashboard?

If anyone can get me a hand that I will be really appreciated.

mouhsinelonly commented 5 years ago

the solution above is not working only the event gets sent but I get no data in e-commerce overview in the dashboard

tnouland commented 5 years ago

I've seen 2 situations where transactions and items did not register:

It seems user IDs are no longer allowed for E-commerce transactions and client IDs must adhere to RCF 4122. See William's answer at https://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid#2040279 for a small function to generate compliant UUID's.

augustomurri commented 5 years ago

Hi all, i have same problems above.. ecommerce data is not tracked on Analitycs

https://pastebin.com/igmP16zb

Just for test, i cloned this script and changed TRACKINGID but is not working https://php-ga-measurement-protocol.readthedocs.io/en/latest/#order-tracking-with-enhanced-e-commerce

Enhanced ecommerce is activated

Debug response seems correct https://pastebin.com/g2hSCpMw

anyone can help me?

ZherebcovSergey commented 5 years ago

Has someone already figured out what could be wrong? Most likely that with the debug mode enabled, no events are sent at all?

initialize and set params code...
$analytics->setDebug(true)->sendEvent();

Although the validation itself sending in this mode says that everything is ok.

Validation

"hitParsingResult": [
    {
        "valid": true,
        "parserMessage": [],
        "hit": "/debug/collect?v=1&tid=myTID&cid=myCID&ti=221243&ta=Online%20payment&tr=779&tt=0&ea=Purchase&pa=purchase&ec=Enhanced%20Ecommerce&ts=0&cu=UAH&t=event&pr1id=tnfT93345-JK3&pr1nm=The%20North%20Face%20Guardian%20Etip%20Glove&pr1ca=perchatki&pr1pr=779.00&pr1br=The%20North%20Face&pr1qt=1"
    }
],
"parserMessage": [
    {
        "messageType": "INFO",
        "description": "Found 1 hit in the request."
    }
]

Send event

"getHttpStatusCode": 200,
"getDebugResponse": {
    "hitParsingResult": [
        {
            "valid": true,
            "parserMessage": [],
            "hit": "/debug/collect?v=1&tid=myTID&cid=myCID&ti=221243&ta=Online%20payment&tr=779&tt=0&ea=Purchase&pa=purchase&ec=Enhanced%20Ecommerce&ts=0&cu=UAH&t=event&pr1id=tnfT93345-JK3&pr1nm=The%20North%20Face%20Guardian%20Etip%20Glove&pr1ca=perchatki&pr1pr=779.00&pr1br=The%20North%20Face&pr1qt=1"
        }
    ],
    "parserMessage": [
        {
            "messageType": "INFO",
            "description": "Found 1 hit in the request."
        }
    ]
}

On debug-page and hit-builder seams ok... ga-validate

ga-hit

miksself commented 5 years ago

Any updates here? E-commerce still not working

clebersantos commented 3 years ago

There is a delay of a few hours for analytics to process the information. After more than 5 hours the information appears

Sadicko commented 3 years ago

I'm using Enhanced E-commerce, try this (change to your data)

$analytics = new Analytics(true);
$amount = $this->currencyConversionToPLN($eventValue, $currency);
$analytics
       ->setProtocolVersion('1')
       ->setTrackingId($trackingId)
       ->setClientId($clientId)
       ->setTransactionId($transactionID)
       ->setRevenue($amount)
       ->setAsyncRequest(true)
       ->setCurrencyCode('PLN');

$analytics->addProduct([
  'sku' => $product->sku,
  'name' => $product->name,
  'category' => $product->category->name,
  'price' => $product->price * $product->vat,
  'quantity' => $product->amount,
  'position' => 1,
]);

// Don't forget to set the product action, in this case to PURCHASE
$analytics->setProductActionToPurchase();

// Finally, you must send a hit, in this case we send an Event
$analytics->setEventCategory('Checkout')
    ->setEventAction('Purchase')
    ->sendEvent();

I have implemented enhanced e-commerce but is not showing on the analytics dashboard. I used the same code but is not working. What must I be doing wrong?

brotherseb commented 3 years ago

Hello ! I've got the same problem, nothing appears on analytics dashboard. Hit is valid, i tried curl on command line too, everything seems ok.

Did you find a way to sort it out ?

Thanks :) Sebastien

brotherseb commented 3 years ago

Hey again !

I found what was missing (even if the Hit Builder was considering my hit was OK and the debug url returned that everything was OK too), the client id (cid) is mandatory. The documentation says (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cid) "This field is required if User ID (uid) is not specified in the request. ", but even with a uid, it does not work (!).

If it can help somebody...

Sebastien