Search code examples
phpelementmetricsadobe-analytics

How to get "metric" and "element" names for PHP - Adobe Analytics


I am pulling a report using the Adobe API from Omniture.

Here is the full script :

    <?php

include_once('/path/SimpleRestClient.php');

// Date 
$end_date = date("Y-m-d",strtotime("-1 days"));
$start_date = date("Y-m-d",strtotime("-8 days"));


// Location of the files exported
$adobe_file = '/path/Adobe_'.$end_date.'.csv';

// List creation that will be updated with the fields and be put into my CSV file
$list = array 
    (
        array('lasttouchchannel', 'product','visits','CTR(Clicks/PageViews)') // headers // ADD or DELETE metrics #
    );

function GetAPIData($method, $data) 
{
    $username       = "XXXX"; 
    $shared_secret  = "XXXX";
    $postURL        = "https://api3.omniture.com/admin/1.4/rest/?method=";  

    // Nonce is a simple unique id to each call to prevent MITM attacks.
    $nonce      = md5(uniqid(php_uname('n'), true));
    // The current timestamp in ISO-8601 format
    $nonce_ts   = date('c');
    /* The Password digest is a concatenation of the nonce, it is timestamp and your password 
    (from the same location as your username) which runs through SHA1 and then through a base64 encoding */
    $digest     = base64_encode(sha1($nonce . $nonce_ts . $shared_secret));

    $rc = new SimpleRestClient();    
    $rc -> setOption(CURLOPT_HTTPHEADER, array("X-WSSE: UsernameToken Username=\"$username\", PasswordDigest=\"$digest\", Nonce=\"$nonce\", Created=\"$nonce_ts\""));
    //var_dump($o);

    $rc -> postWebRequest($postURL .$method, $data);    
    return $rc;
}

$method = 'Report.Queue';   
$data       ='
            {
                "reportDescription":
                {
                "reportSuiteID":"XXXX",
                "dateFrom":"'.$start_date.'",
                "dateTo":"'.$end_date.'",
                "metrics":[{"id":"visits"},{"id":"instances"},{"id":"pageviews"}],
                "elements":[{"id":"lasttouchchannel","top":"50000"}]
                }
            }';
/*
    "date":"'.$date.'",
    "dateTo":"'.$date.'",
"dateFrom":"'.$start_date.'",
"dateTo":"'.$end_date.'",
*/          
$rc=GetAPIData($method, $data);

if($rc -> getStatusCode() == 200) // status code 200 is for 'ok'
{
    $counter    = 0; 
    do
    {
        if($counter>0){sleep($sleep = 120);}
        $return = GetAPIData('Report.Get', $rc->getWebResponse());
        $counter++;
    }while($return -> getStatusCode() == 400 && json_decode($return->getWebResponse())->error == 'report_not_ready'); // status code 400 is for 'bad request'

    // 
    $json=json_decode($return->getWebResponse());

    foreach ($json->report->data as $el) 
    {
        echo $el->name.":".$el->counts[0].":".$el->counts[1]."\n";

        // Adding the data in the CSV file without overwriting the previous data
        array_push($list, array($el->name, $el->name, $el->counts[0], ($el->counts[1])/($el->counts[2])));
    }   

}
else
{
    echo "Wrong";
}

$fp = fopen($adobe_file, 'w');

foreach ($list as $fields) 
{
    // Save the data into a CSV file
    fputcsv($fp, $fields);
}

fclose($fp);

?>

How can I get the names of the metrics and elements in order to use them in this script? There is no way. I searched with all the possible tags on google and nothing worked !

I need the metrics and elements for this part of the code :

$data       ='
            {
                "reportDescription":
                {
                "reportSuiteID":"XXXX",
                "dateFrom":"'.$start_date.'",
                "dateTo":"'.$end_date.'",
                "metrics":[{"id":"visits"},{"id":"instances"},{"id":"pageviews"}],
                "elements":[{"id":"lasttouchchannel","top":"50000"}]
                }
            }';

I cannot find 'date' as an element which is crucial. I cannot find all the other metrics as well. In Google Analytics we had this link : Google Analytics Query

but in Adobe there is not any. I want something like that :

"metrics":[{"id":"instances"},{"id":"impressions"}],
                "elements":[{"id":"date","top":"50000"}]

Solution

  • You would json_decode() as $data contains a JSON string. For example:

    $data ='
                {
                    "reportDescription":
                    {
                    "reportSuiteID":"XXXX",
                    "dateFrom":"'.$start_date.'",
                    "dateTo":"'.$end_date.'",
                    "metrics":[{"id":"visits"},{"id":"instances"},{"id":"pageviews"}],
                    "elements":[{"id":"lasttouchchannel","top":"50000"}]
                    }
                }';
    
    
    $json = json_decode($data, true);
    echo $json['reportDescription']['dateFrom']; 
    print_r($json['reportDescription']['metrics']);