(400) Unknown metric(s): ga:socialNetwork' - php

I need get acquisition->channels info. I think I can get it using "ga:socialNetwork" dimension. But when I use it I get the following error:
(400) Unknown metric(s): ga:socialNetwork
However, ga:socialNetwork dimension exists: https://developers.google.com/analytics/devguides/reporting/core/dimsmets
My service call:
$service->data_ga->get('ga:xxxxx', '2010-01-01', '2015-01-15', 'ga:socialNetwork', $optParams);

ga:socialNetwork is a dimension not a metric. You are attempting pass in a dimension to the parameter that expects a metric.
Attempt the following to correct your code:
$optParams = array(
'dimensions' => 'ga:socialNetwork',
'sort' => '-ga:sessions');
$service->data_ga->get(
'ga:xxxxxx',
'2010-01-01',
'2010-01-15',
'ga:sessions',
$optParams);
ga:sessions is a metric where ga:socialNetwork is a dimension, this help center article should help clarify the difference.

Related

Google Cloud Storage paginate objects in a bucket (PHP)

I want to iterate over the objects in a bucket. I REALLY need to paginate this - we have 100's of thousands of objects in the bucket. Our bucket looks like:
bucket/MLS ID/file 1
bucket/MLS ID/file 2
bucket/MLS ID/file 3
... etc
Simplest version of my code follows. I know the value I'm setting into $params['nextToken'] is wrong, I can't figure out how or where to get the right one. $file_objects is a 'Google\Cloud\Storage\ObjectIterator', right?
// temp: pages of 10, out of a total of 100. I really want pages of 100
// out of all (in my test bucket, I have about 700 objects)
$params = [
'prefix' => $mls_id,
'maxResults' => 10,
'resultLimit' => 100,
'fields' => 'items/id,items/name,items/updated,nextPageToken',
'pageToken' => NULL
];
while ( $file_objects = $bucket->objects($params) )
{
foreach ( $file_objects as $object )
{
print "NAME: {$object->name()}\n";
}
// I think that this might need to be encoded somehow?
// or how do I get the requested nextPageToken???
$params['pageToken'] = $file_objects->nextResultToken();
}
So - I don't understand maxResults vs resultLimit. It would seem that resultLimit would be the total that I want to see from my bucket, and maxResults the size of my page. But maxResults doesn't seem to affect anything, while resultLimit does.
maxResults = 100
resultLimit = 10
produces 10 objects.
maxResults = 10
resultLimit = 100
spits out 100 objects.
maxResults = 10
resultLimit = 0
dumps out all 702 in the bucket, with maxResults having no effect at all. And at no point does "$file_objects->nextResultToken();" give me anything.
What am I missing?
The objects method automatically handles pagination for you. It returns an ObjectIterator object.
The resultLimit parameter limits the total number of objects to return across all pages. The maxResults parameter sets the maximum number to return per page.
If you use a foreach over the ObjectIterator object, it'll iterate through all objects, but note that there are also other methods in ObjectIterator, like iterateByPage.
Ok, I think I got it. I found the documentation far too sparse and misleading. The code I came up with:
$params = [
'prefix' => <my prefix here>,
'maxResults' => 100,
//'resultLimit' => 0,
'fields' => 'items/id,items/name,items/updated,nextPageToken',
'pageToken' => NULL
];
// Note: setting 'resultLimit' to 0 does not work, I found the
// docs misleading. If you want all results, don't set it at all
// Get the first set of objects per those parameters
$object_iterator = $bucket->objects($params);
// in order to get the next_result_token, I had to get the current
// object first. If you don't, nextResultToken() always returns
// NULL
$current = $object_iterator->current();
$next_result_token = $object_iterator->nextResultToken();
while ($next_result_token)
{
$object_page_iterator = $object_iterator->iterateByPage();
foreach ($object_page_iterator->current() as $file_object )
{
print " -- {$file_object->name()}\n";
}
// here is where you use the page token retrieved earlier - get
// a new set of objects
$params['pageToken'] = $next_result_token;
$object_iterator = $bucket->objects($params);
// Once again, get the current object before trying to get the
// next result token
$current = $object_iterator->current();
$next_result_token = $object_iterator->nextResultToken();
print "NEXT RESULT TOKEN: {$next_result_token}\n";
}
This seems to work for me, so now I can get to the actual problem. Hope this helps someone.

PHP: It wants an "object" so I give it and object and then it errors saying it wanted an "array" what gives?

Im confused. Working with the Cardano API (v1) using a PHP wrapper.
The createNewTransaction($source, $destination, $spendingPassword) function requires parameters in the form of an array:
// source of funds (array)
$source = array(
'accountIndex' => $wallet_idx,
'walletId' => $wallet_id
);
// destination for funds (array)
$destination = array(
'address' => $banker,
'amount' => $lovelace
);
// spending pass
$spendingPassword = get_user_meta('1', 'spending_pass', true);
// transaction
$client->createNewTransaction($source, $destination, $spendingPassword);
So then im returned this error from the API:
Array ( [status] => error [diagnostic] => Array (
[validationError] => Error in $: When parsing the constructor Payment
of type Cardano.Wallet.API.V1.Types.Payment expected Object but got
Array. ) [message] => JSONValidationFailed )
So then, I looked up how to turn an array into an object thinking that would solve my problem. So I did this:
$o_source = (object) $source;
$o_destination = (object) $destination;
and then sent it back to the API to see if the transaction would go through, but I got another error:
Fatal error: Uncaught TypeError: Argument 1 passed to
Cardano::createNewTransaction() must be of the type array, object
given...
So, now Im confuseed, I thought I had originally sent it an array but then it said it wanted an object. I dont get it!
Help please!
The code from the linked library looks something like:
public function createNewTransaction(
array $source,
array $destination,
string $spendingPassword
): array {
$groupPolicy = 'OptimizeForSecurity';
return self::jsonDecode($this->post(
'/api/v1/transactions' .
'?source=' . $source .
'?destination=' . $destination .
'?groupingPolicy=' . $groupPolicy .
'?spendingPassword=' . $spendingPassword
), true);
}
If you run this code, then $source and $destination would be cast from arrays to the string "Array". This is what the API is complaining about, it probably expected something other than the string "Array" as the value of the source parameter.
I recommend that you look for another library, or implement the API calls yourself, because this library has no chance of working correctly in its current state.

How to add dimensions to Google analytics api requets

The examples given by Google are a very simple query request. however, when I try to add more parameters, it will throw errors and I don't exactly know how to structure the syntax.
This simple query request works:
return $analytics->data_ga->get(
'ga:' . $viewID,
$startDate,
$endDate,
'ga:sessions'
);
I need more information and I've already used Google's Query Explorer to get the Information but I just don't know how to structure my PHP query. The Information I want to request is also ga:pageviews as another metric, ga:pagePath and ga:pageTitle as dimensions and also a filter. I already fail at adding a second metric.
I have tried this:
return $analytics->data_ga->get(
'ga:' . $viewID,
$startDate,
$endDate,
'ga:sessions',
'ga:pageviews'
);
simply adding it doesn't work. Can anyone point me in the correct direction?
Dimensions need to be added as option parms
//Adding Dimensions
$params = array('dimensions' => 'ga:userType');
// requesting the data
$data = $service->data_ga->get("ga:89798036", "2014-12-14", "2014-12-14", "ga:users,ga:sessions", $params );

Google Analytics, Dimensions & Metrics - How to?

I've finally figured out how i connected to the Google Analytics, correct - and I'm now able to access data to some point. I'm using the google-api-php-client.
I can work with metrics just fine fx, by doing
return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:sessions,ga:pageviews,ga:sessionDuration');
which will return me number of sessions, pageviews, and session duration. But now lets say I am interested in using some of the dimensions as well - Maybe i want the query return site usage data for all traffic by search engine, sorted by pageviews in descending order.
dimensions=ga:source
metrics=ga:pageviews,ga:sessionDuration,ga:exits
filters=ga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==organic,ga:medium==ppc
sort=-ga:pageviews
the data_ga->get function calls for the following parameters: $ids, $startDate, $endDate, $metrics, $optParams = array()
I tried supplying the dimensions and filters in a array, but it returns me the following errors
Warning: Illegal string offset 'type' in
xxxxxxxxx/src/Google/Service/Resource.php on line 269
Warning: Illegal string offset 'location' in
xxxxxxxxx/src/Google/Service/Resource.php on line 272
Warning: Illegal string offset 'location' in
xxxxxxxxx/src/Google/Service/Resource.php on line 274
Dimensions are not required so there for they are part of option parameters.
//Adding Dimensions
$params = array('dimensions' => 'ga:userType');
// requesting the data
$data = $service->data_ga->get("ga:89798036", "2014-12-14", "2014-12- 14", "ga:users,ga:sessions", $params );
Filters and sort can also be added to the $parms array

ZF2 Zend\Db Insert/Update Using Mysql Expression (Zend\Db\Sql\Expression?)

Is there any way to include MySQL expressions like NOW() in the current build of ZF2 (2.0.0beta4) through Zend\Db and/or TableGateway insert()/update() statements?
Here is a related post on the mailing list, though it hasn't been answered: http://zend-framework-community.634137.n4.nabble.com/Zend-Db-Expr-and-Transactions-in-ZF2-Db-td4472944.html
It appears that ZF1 used something like:
$data = array(
'update_time' => new \Zend\Db\Expr("NOW()")
);
And after looking through Zend\Db\Sql\Expression I tried:
$data = array(
'update_time' => new \Zend\Db\Sql\Expression("NOW()"),
);
But get the following error:
Catchable fatal error: Object of class Zend\Db\Sql\Expression could not be converted to string in /var/www/vendor/ZF2/library/Zend/Db/Adapter/Driver/Pdo/Statement.php on line 256
As recommended here: http://zend-framework-community.634137.n4.nabble.com/ZF2-beta3-Zend-Db-Sql-Insert-new-Expression-now-td4536197.html I'm currently just setting the date with PHP code, but I'd rather use the MySQL server as the single source of truth for date/times.
$data = array(
'update_time' => date('Y-m-d H:i:s'),
);
Thanks, I appreciate any input!
Zend_Db_Sql_Expression works for me now.
$data = array(
'update_time' => new \Zend\Db\Sql\Expression("NOW()"),
);
I am not sure if they fixed this in the latest updates, but it was a know bug and even if they said it was fixed, for me it still didn't work after the fix.
It works for me too. Maybe, your problem was like mine. The string error conversion was because the parameter that I passed to method was elements of array and not the array. Check this and have sure that you are passing $object->method($array).
You can try:
\Zend\Db\Sql\Predicate\Literal();
//or
\Zend\Db\Sql\Literal();
------------------------------------
$data = array(
'update_time' => new \Zend\Db\Sql\Literal("NOW()"),
);
https://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html#literal-literal

Categories