I am execting following GET request to obtain data form wordpress
http://domain:port/wp-json/wp/v2/announcement?_fields=announcement_category,effective_start_date,effective_end_date
The result is as below
[
{
"announcement_category": [
216
],
"effective_start_date": "2020-03-27",
"effective_end_date": "2020-04-16"
},
{
"announcement_category": [
215
],
"effective_start_date": "2020-03-25",
"effective_end_date": "2020-03-31"
}]
I need to sort/order my response in the ASC order of the "announcemet_category" value. (there is always one value for this array)
I know "orderby" does not support meta_values.
How to enable meta_value sorting and how can I execute my query for this.
Please help
You should be able to sort on a meta value using:
$q = new WP_Query(
// Other query params e.g. post type...
array(
'meta_key' => 'announcement_category',
'orderby' => 'meta_value_num',
'order' => 'ASC',
)
);
Based on this answer.
However I think your issue is that as you've pointed out, announcement_category is an array rather than a number, which you won't be able to sort on.
If you can't change announcement_category to a number rather than an array, you can use a usort to sort the results of your WordPress query, before sending the API response:
$arr1 = array(
array('announcement_category'=>array(216)),
array('announcement_category'=>array(218)),
array('announcement_category'=>array(202)),
array('announcement_category'=>array(300)),
);
function sortByOrder($a, $b) {
return $a['announcement_category'][0] - $b['announcement_category'][0];
}
usort($arr1, 'sortByOrder');
print_r($arr1); // Ordered ascendingly
Alternatively, you can use javascript on the client side to sort the response as required:
announcements.sort(function (a, b) {
return a[announcement_category][0] - b[announcement_category][0];
});
Array Sort
Related
I'm adding GTM and GA4 to some website apps that need to produce detailed stats on the click-throughs of ads per advertiser.
This looks infeasible with standard GA reporting so am using the PHP implementation of Analytics Data API V1 Beta. Since there are few examples (eg analyticsdata/quickstart.php) of V1 reporting using PHP, I am translating other classes and operands from the REST API’s JSON .
<?php
namespace Google\Analytics\Data\V1beta;
require 'vendor/autoload.php';
$property_id = '<redacted>';
putenv('GOOGLE_APPLICATION_CREDENTIALS=Keyfile.json');
$client = new BetaAnalyticsDataClient();
// Make an API call.
$response = $client->runReport([
'property' => 'properties/' . $property_id,
'dateRanges' => [
new DateRange([
'start_date' => '2021-04-01',
'end_date' => 'today',
]
),
],
'dimensions' => [new Dimension(
[
'name' => 'customEvent:link_classes'
]
),
],
'dimensionFilter'=>[new FilterExpression(
[
'filter'=>[new Filter(
[
'field_name' => 'customEvent:Classes',
'string_filter' => [new Filter\StringFilter(
[
'match_type'=> '1',
'value'=> 'AdvertA',
'case_sensitive'=> false
])]])]])],
'metrics' => [new Metric(
[
'name' => 'eventCount',
]
)
]
]);
etc
The Quickstart example works but has endless trouble when a dimensionFilter is added.
For example, match_type should be an enum of one of a few character strings (EXACT, CONTAINS and so on). The JSON definition of match_type only shows the strings (enum 'members') and not any associated values (which would usually be integers). The GA4 migration guide has an example with
"matchType": "BEGINS_WITH"
PHP doesn’t have ‘enum’ but the equivalent would be to select one string and assign it to match_type (vide above). Wrong: StringFilter falls over unless it is given an integer operand, presumably the ordinal number of the desired match in the enum match string (and is the first one 0 or 1?). My understanding of the JSON schema was that an 'enum' list simply restricted the result to one of the unique operands, with an optional check on the operand type. (By comparison, the Python enumerate function returns an object containing a list of pairs with the ordinal number of an operand preceding the operand).
Custom dimensions appear not to conform to the API’s JSON. In Analytics, I specify a custom dimension with a dimension Name of Classes and User Property/Parameter of link_classes**.
However... in the API, dimension Name has to be customEvent:link_classes and not customEvent:Classes. Otherwise it falls over with ‘Field customEvent:Classes is not a valid dimension’
This occurs also when defining field_name in a Filter within a Filter Expression.
So is the API dimension Name not the name of the Analytics dimension Name but actually the Property/Parameter of an Analytics descriptive name? In one place I read the latter: "Custom dimensions are specified in an API report request by the dimension's parameter name and scope." but elsewhere it is implied that Name is the dimension name, e.g. /devguides/reporting/data/v1/advanced:
"dimensions": [{ "name": "customUser:last_level" }]
Finally, even falling in line with what the developers have implemented, dimensionFilter falls over with ‘Expect Google\Analytics\Data\V1beta\Filter\StringFilter’
It is Beta code but one would not expect overt deviations from the REST spec so perhaps I am reading the spec wrongly. Does anyone else have this problem?
** GTM has a ‘Click - Just Links’ trigger where the ‘click URL’ ‘contains’ the advertiser’s URL. The Classes custom dimension in the API dimension Filter has the class values of the adverts click-through links.
To answer the first part of your question, I believe the correct way to use an enum in PHP would be:
'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH
As for the second question. Per the API schema documentation the name of a custom dimension is constructed as customEvent:parameter_name for event scoped dimensions and customUser:parameter_name for user scoped dimensions.
Where parameter_name, as you correctly noted, is not a descriptive name, but rather the event parameter name. In your example you seem to be using a user scoped dimension, so the dimension name in the API should be customUser:link_classes.
Here is a complete example that seems to be running fine:
require 'vendor/autoload.php';
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\FilterExpression;
use Google\Analytics\Data\V1beta\Filter;
use Google\Analytics\Data\V1beta\Metric;
/**
* TODO(developer): Replace this variable with your Google Analytics 4
* property ID before running the sample.
*/
$property_id = 'YOUR-GA4-PROPERTY-ID';
$client = new BetaAnalyticsDataClient();
// Make an API call.
$response = $client->runReport([
'property' => 'properties/' . $property_id,
'dateRanges' => [
new DateRange([
'start_date' => '2020-03-31',
'end_date' => 'today',
]),
],
'dimensions' => [new Dimension(
[
'name' => 'customUser:link_classes'
]),
],
'dimensionFilter' => new FilterExpression(
[
'filter' => new Filter(
[
'field_name' => 'customUser:link_classes',
'string_filter' => new Filter\StringFilter(
[
'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH,
'value' => 'AdvertA',
'case_sensitive' => false
]
)
])
]),
'metrics' => [new Metric(
[
'name' => 'eventCount',
]
)
]
]);
Many thanks Ilya for a most useful, prompt and correct reply.
Three points:
1.
Using
'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH
instead of:
'match_type' => ‘BEGINS_WITH’
fixes the problem of “Uncaught Exception: Expect integer. in /vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php” as the
MatchType::BEGINS_WITH (etc) constant returns an integer (in this case 2) from class MatchType.
2.
It would forestall errors if a reminder were added to the places in Custom Dimension documentation where dimension Name is used, such as
/devguides/reporting/data/v1/advanced: "dimensions": [{ "name": "customUser:last_level" }]
emphasising that name is not the dimension Name as defined to Analytics but rather the associated User Property/Parameter name. Or perhaps the Name heading in GA's Custom Dimension 'form' should be amended.
3.
Finally, dimensionFilter falling over with
‘Expect Google\Analytics\Data\V1beta\Filter\StringFilter’
error message was caused by my stupidity in instantiating FilterExpression, Filter and StringFilter as if they were array elements,
e.g
'string_filter' => [new Filter\StringFilter(
rather than
'string_filter' => new Filter\StringFilter(
something that is an unfortunately easy mistake to make when carelessly following the sample report code, where dateRanges, dimensions and so on correctly define arrays (since they can take multiple date ranges and dimensions), e.g.
'dimensions' => [new Dimension (
using Symfony 5 and Omines, I have 2 tables sending a request to the same url:
the first table show products sold this month,
the second table show products sold before this month
Therefore, I want to pass as a parameter a date. and then in the query_builder filter it.
I do not find any hint in the documentation.
Any help would be great.
Thanks,
add your desired variable which you want to use in querybuilder like shown in example below
$table->createAdapter(ORMAdapter::class, [
'entity' => Product::class,
'query' => function (QueryBuilder $builder) use ($date) {
$builder
->select('p')
->from(Product::class, 'p')
->where("p.date = :date")
->setParameter("date", $date)
;
},
]);
Is it possible to have status as active and trialing both?
Following code works for active subscription but I also want to retrieve data having status as "trialing".
$all_subs = \Stripe\Subscription::all(
array(
"plan" => $group_plan_id,
"status" => "active",
'limit' => 100
)
);
The documentation specifically says "one of...", so you can only choose one status per call. You will need to either make two calls with each status type, or use the status "all" to get ALL of the types (which will also return cancelled, past_due, and unpaid) and then filter out the ones you don't want.
$all_subs = \Stripe\Subscription::all(
array(
"plan" => $group_plan_id,
"status" => "all",
"limit" => 100
)
);
$statuses = array('active','trialing');
foreach ($all_subs->data as $sub) {
if (!in_array($sub->status,$statuses)) {
continue;
}
// do some code here
}
Source: Stripe API Reference
I want to push the products profit of a transaction to Google Analytics.
With this i want to generate a report where i can see the total Product revenue and it's profit for each product that has been ordered.
i'm using this api to push custom metrics to analytics (Measurement protocol api)
I have made several custom metrics in analytics, for example:
Productsprofit (Scope hit, decimal)
Productsprofit2 (Scope Product, decimal)
I've googled several times and i think i need to use the Scope Hit for this type of tracking, am i right?
I've used the api to push the metric with the right indexes to Analytics
$analytics->setTransactionId('test'.$orderId)
->setAffiliation('Testshop')
->setRevenue(number_format($orderTotalPrice,2,'.',''))
->setTax(number_format($taxTotal,2,'.',''))
->setShipping(number_format($shippingTotal,2,'.',''))
->setCouponCode('');
foreach($orderInfo['orderDetails'] as $orderDetail) {
// Include a product, only required fields are SKU and Name
$productData1 = [
'sku' => $orderDetail['model'],
'name' => $orderDetail['name'],
'brand' => '',
'category' => '',
'variant' => '',
'price' => number_format($orderDetail['price'], 2, '.', ''),
'quantity' => $orderDetail['aantal'],
'coupon_code' => '',
'position' => 0,
'custom_metric_3'=>50.45, //<-- test data (Hit)
'custom_metric_4'=>15.50 //<-- test data (Product)
];
$analytics->addProduct($productData1);
}
// 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've created a custom report and added these metric to my report. But both metrics stay empty in my report.
Does anyone know what i'm doing wrong ? or what might solve my problem?
Thanks in advance!
In the PHP library that you are using, custom metrics and dimensions are set in a different manner depending on the scope, for Product scope you use the product array like you did, for Hit scope you use the setCustomMetric method. In your code this would look like:
$analytics->setTransactionId('test'.$orderId)
->setAffiliation('Testshop')
->setRevenue(number_format($orderTotalPrice,2,'.',''))
->setTax(number_format($taxTotal,2,'.',''))
->setShipping(number_format($shippingTotal,2,'.',''));
foreach($orderInfo['orderDetails'] as $orderDetail) {
$productData1 = [
'sku' => $orderDetail['model'],
'name' => $orderDetail['name'],
'brand' => 'the brand',
'category' => 'test category',
'variant' => 'variant x',
'price' => number_format($orderDetail['price'], 2, '.', ''),
'quantity' => $orderDetail['qty'],
'coupon_code' => 'coupon used',
'custom_metric_4' => 15.50,
];
$analytics->addProduct($productData1);
}
$analytics->setProductActionToPurchase();
$analytics->setCustomMetric(50.45, 3);
$analytics->setEventCategory('Checkout')
->setEventAction('Purchase')
->sendEvent();
Notice the call to $analytics->setCustomMetric(50.45, 3); before sending the hit. This should send the right data, the rest is creating or looking at the right report in Google Analytics.
While I was trying to show pictures I made this pleasant discovery in backpack:
$this->crud->addFields([
[ // Upload
'name' => 'pictures', //<-- this is an Entity
'label' => 'Photos',
'type' => 'upload_multiple',
'upload' => true,
'disk' => 'uploads'
]
]);
This fragment of code gives me this:
My questions is:
What am I doing wrong?
How can get the value 'file' of this two vectors? I need this for display the images.
You probably forgot to add 'photos' to the cast array.
Based on the documentation:
Step 3. Since the filenames are stored in the database as a JSON array, we're going to use attribute casting on your model, so every time we get the filenames array from the database it's converted from a JSON array to a PHP array:
protected $casts = [
'photos' => 'array'
];
https://laravel-backpack.readme.io/docs/crud-fields#upload_multiple