EyeSeeT
EyeSeeT

Reputation: 11

How to send ad revenue to GA4 using a measurement protocol event?

I'm trying to get the ad revenue from a external source into the GA4 "Monetisation" -> "Publisher Ads" report by sending an event using the measurement protocol from php. However, I can't get this to work. I've tried several events marked or not as conversions.

The closest I've gotten so far is by sending thhe "ad_impression" event with the function below. This function is based on the three links in the list. The event is shown in the correct report. However, the field "Total ad revenue" shows 0.00, and the other ad_ parameters are not set. If I add "Event Value" to the metrics or the report I see the (revenue) values in that column.

sources:

I hope someone can help me create a correct event to get the ad revenue in the publisher ads report.

<?php
function send_ad_revenue($measurement_id, $api_secret, $client_id, $revenue) {
    $params = array (
        'ad_platform' => 'my-platform',
        'ad_format' => 'banner',
        'ad_source' => 'my-source',
        'ad_unit_name' => 'sidebar',
        'currency' => 'EUR',
        'value' => round(floatval($revenue), 2)
    );
    $data = array(
        'client_id' => $client_id,
        'events' => array(
            array(
                'name' => 'ad_impression',
                'params' => $params
            )
        )
    );
    $payload = json_encode($data);
    $ch = curl_init();
    
    // curl_setopt($ch, CURLOPT_URL, "https://www.google-analytics.com/debug/mp/collect?measurement_id={$measurement_id}&api_secret={$api_secret}");
    curl_setopt($ch, CURLOPT_URL, "https://www.google-analytics.com/mp/collect?measurement_id={$measurement_id}&api_secret={$api_secret}");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    $response = curl_exec($ch);
    return $response;
}
?>

Upvotes: 1

Views: 610

Answers (1)

ali izadi
ali izadi

Reputation: 549

It seems that your curl code is missing a currency parameter, which is causing an issue. To resolve this, you can add a parameter and set a constant currency value, such as USD. However, make sure that the currency you choose is acceptable by Google. Here are some important things to keep in mind while adding the 'currency' parameter:

  • Use the 3-letter ISO 4217 format to assign a string rather than a number to the 'currency' parameter.
  • Assign the 'currency' parameter at the event level of the purchase event.
  • Spell the 'currency' parameter correctly and use all lowercase letters.
  • Surround the 'currency' parameter with quotation marks followed by a trailing comma.

Upvotes: 0

Related Questions