laravel / blog-contest-may-mayhem

240 stars 16 forks source link

Track the usage of your Laravel APIs with the Measurement Protocol #31

Open stefanzweifel opened 6 years ago

stefanzweifel commented 6 years ago

https://stefanzweifel.io/posts/track-the-usage-of-your-laravel-apis-with-the-measurement-protocol

ghost commented 6 years ago

@stefanzweifel Awesome article you wrote, thank you for sharing :+1:

andreladocruz commented 6 years ago

@stefanzweifel,

Loved your article!!! Thanks!!!

Do you have any documentation about sending GA hits to others domains?

I've applied Measurement Protocol hit in our application but it's not been captured in our client's GA.

Any clue?

stefanzweifel commented 6 years ago

Thanks for the kind words.

@andreladocruz Have you used the Tracking ID (The UA-xxxxx thing) of your client? Google Analytics doesn't have a setting or such for domains. In theory you could point many domains to one single GA property.

andreladocruz commented 6 years ago

@stefanzweifel,

We are trying to post information for different IDs (The UA-xxxxx thing) from the same server.

Our GA get's the hits. But our clients GA don't.

Here is the code we are using:

` private function sendConfirmedTransaction(Transaction $transaction) { $sale = [ 'v' => self::VERSION, 'tid' => $this->code, 'dh' => $this->domain, 'cid' => $transaction->contact_id ?? 555, 'ds' => 'guru', 'qt' => Carbon::now()->diffInSeconds($transaction->ordered_at) * 1000, 't' => 'transaction', 'ti' => $transaction->marketplace_id, 'ta' => e($transaction->affiliate_name), 'tr' => $transaction->value, 'tt' => ($transaction->value - $transaction->netValue), 'cu' => $transaction->currency ];

    if($transaction->tracking){
        $sale['cn'] = e($transaction->tracking->name);
        $sale['cs'] = e($transaction->tracking->publisher);
        $sale['ci'] = e($transaction->tracking->id);
    }

    if($transaction->utm){
        $sale['cn'] = empty($transaction->utm->utm_campaign) ? $sale['cn'] : e($transaction->utm->utm_campaign);
        $sale['cm'] = e($transaction->utm->utm_medium);
        $sale['ck'] = e($transaction->utm->utm_term);
        $sale['cc'] = e($transaction->utm->utm_content);
    }

    $item = [
        'v' => self::VERSION,
        'tid' => $this->code,
        'dh' => $this->domain,
        'cid' => $transaction->contact_id ?? 555,
        'ds' => 'guru',
        'qt' => Carbon::now()->diffInSeconds($transaction->ordered_at) * 1000,
        't' => 'item',
        'ti' => $transaction->marketplace_id,
        'in' => e($transaction->product_name),
        'ip' => $transaction->value,
        'iq' => $transaction->product_qty,
        'ic' => $transaction->product_marketplace_id,
        'cu' => $transaction->currency
    ];

    $this->post($sale, true);
    $this->post($item, true);

    $this->post($sale);
    $this->post($item);
}

private function post($data, $debug = false)
{
    $url = self::URL;

    if ($debug){
        $url = self::URL_DEBUG;
    }

    $results = $this->guzzleClient->post($url, ['form_params'=> $data]);
    $results = json_decode($results->getBody()->getContents(),true);

    if($debug && ! $results['hitParsingResult'][0]['valid'])
    {
        $message = 'Erro ao enviar o hit de testes para "';
        $message .= $results['hitParsingResult'][0]['hit'];
        $message .= '". Mensagem de erro: "';
        $message .= $results['hitParsingResult'][0]['parserMessage'][0]['description']. '".';
        throw new HitErrorException($message);
    }
}

`

We really don't know why it's not showing in our clients GA.

There is no error message when hitting the debug endpoint. =(

stefanzweifel commented 6 years ago

Hm, if never used GAMP in such a way. 😅 I can only suggest you try to debug/output the $data-variable in your post method, and test if it actually contains the correct Tracking ID.

Maybe try posting on the GA supports forums or StackOverflow? This issue thread is probably not the best place.

andreladocruz commented 6 years ago

@stefanzweifel ,

As we sent first to debug endpoint, if there was an error, it would throw an exception.

I'll continue to dig deeper and make it works. =)

Thanks again. <3

cashlion commented 6 years ago

@andreladocruz Does your client's GA properties have the bot exclusion setting enabled in their Views. I've ran into this issue before and that was the issue. Would recommend checking an Unfiltered/Unaltered View or create a new Test-only View with the setting disabled before altering any Production Views.

andreladocruz commented 6 years ago

@cashlion,

As we are sending only e-commerce hits, I don't think this is the case.

But I'll check this. =)

Thanks