How to get unique users in a given period of time in Google Analytics through API? - google-analytics-api

How do I get unique users in a given period of time in Google Analytics through API?
For example, I want to get unique users from 2022-10-01 to 2022-10-12.
I know there's a feature called 30 Day Active Users in GA, but it is impossible to calculate only for the given period (12 days, not 30 days)

You can use dataRange attribute with metrics and dimensions to filter the specific date visitors
For example
'json' => [
'reportRequests' => [
'viewId' => "viewId",
'dateRanges' => [
"startDate" => "2022-10-01",
"endDate" => "2022-10-12"
],
'dimensions' => ['name' => 'ga:date'],
'metrics' => ['expression' => 'ga:visitors'],
]
]
To find more about metrics and dimensions refer this https://ga-dev-tools.web.app/dimensions-metrics-explorer/

Related

geoNear with Pagination and sorting on created_at

I´m having an App with the capability of listing articles within a user specified location + radius surrounding. See ruby code with mongo query below.
For Web App, it uses pagination based on "last item" (min_distance) and "excluded_ids" in combination with "limit", what I believe to be the most performant and also better than "limit" and "skip". This solution approach supports "previous page" and "next page". For "jump to page" it makes use of the combination of "limit" and "skip".
geoNear by nature sorts by "distance".
New requirement is to have, additionally, sort by "created_at" (user toggle).
My question:
For sort by "created_at" to work, will it be sufficient to add sort into the query? I´m afraid this won´t work for the current implemented pagination approach and I will need to somehow rewrite to have "last item" based on "created_at", rather than on "min_distance".
Can someone tell for sure how Mongo handles internally the sort and if simply adding sort for created_at will work with this type of pagination?
Or else, how this would look for replacing mind_distance for created_at.
Also, for supporting both sort types, what index/es is/are needed?
One for both, or one for each?
def self.articles_with_distance(params, excluded_ids=[])
if params[:min_distance].present?
Article.collection.aggregate([
{ :"$geoNear" => {
:near => {
:type => "Point",
:coordinates => [params[:lon].to_f, params[:lat].to_f],
},
:query => {_id: { :"$nin" => excluded_ids }}, # to exclude items already shown if min distance between two pages are same
:distanceField => "distance_from",
:maxDistance => params[:radius].to_i,
:minDistance => params[:min_distance].to_i, # only need to reduce the result set by exclude items had been shown previous pages
:spherical => true,
:limit => (params[:per_page].to_i || 10)
}}
]);
else
Article.collection.aggregate([
{ :"$geoNear" => {
:near => {
:type => "Point",
:coordinates => [params[:lon].to_f, params[:lat].to_f],
},
:distanceField => "distance_from",
:maxDistance => params[:radius].to_i,
:spherical => true,
#
}},
{
:"$skip" => (params[:per_page].to_i * (params[:page].to_i-1))
},
{
:"$limit" => (params[:per_page].to_i || 10)
}
]);
end
end

paypal trial recurring payment not getting started

I have created a paypal recurring payment with a trial period of 1 month. I am testing with my sandbox account.
After completing 1 payment I can see in my sandbox merchant account that the recurring payment has been created. But it's displaying: "recurring cycle remaining 1 month" and I have received $0. I mean the recurring payment has been started, but I am not receiving the money.
Is that paypal's fault or is there a problem with my code?
Well I am using a CMS(Social Eninge) . Paypal has been integrated here already . So I am just modifying it. Here is the parameters I am sending.
// This Portion to display the payment summery :
$params['driverSpecificParams']['PayPal'] = array(
'ITEMCATEGORY'=> 'Digital',
'AMT' => $params['price'],
'NAME' => $package->title,
'DESC' => $desc,
'ITEMS' => array(
array(
'NAME' => $package->title,
'AMT' => $params['price'],
'QTY' => 1,
),
),
'BILLINGTYPE' => 'RecurringPayments',
'BILLINGAGREEMENTDESCRIPTION' => $desc,
);
// And this portion for payment subscription.
$rpData = array(
'TOKEN' => $params['token'],
'PROFILEREFERENCE' => $order->order_id,
'PROFILESTARTDATE' => $data['TIMESTAMP'],
'DESC' => $desc, //Item Description
'BILLINGPERIOD' => ucfirst($package->recurrence_type),
'BILLINGFREQUENCY' => $package->recurrence,
'INITAMT' => 0 ,
'TRIALBILLINGPERIOD' => ucfirst($package->recurrence_type),
'TRIALBILLINGFREQUENCY' => $package->recurrence,
'TRIALTOTALBILLINGCYCLES' => 1,
'TRIALAMT' => $_SESSION["price"],
'AMT' => $package->price,
'CURRENCYCODE' => $this->getGateway()->getCurrency(),
);
I would also like to know whether setting $0 for a few months in my trial period will work or not ?
Paypal displaying like this :
[1]: https://i.stack.imgur.com/mSxk1.png
Well, I have found my code is right. It's paypal's delay for few hours.
Also setting $0 for trial period works perfectly. Trial period automatically choose the starting interval. You need to pass for how many days ,months, year, or week you wanna offer trial and which price, it might be lower price or zero doesn't matters.Like I am offering 1st month free/some reduced price after that it will continue the normal price.

Facebook Marketing API connections: exclude people

So on the Ads Manager under connections, it gives you the choice to exclude people who used your app as a way to avoid showing ads to your current users.
The marketing API in the doc says you can set a targeting spec: 'app_install_state' which can be set to 'INSTALLED' or 'NOT_INSTALLED' and to quote facebook
must be used in conjunction with promoted_object at adset level
I have tried adding it to the $targeting object as well as adding it on the adset level. But when I go to view the ad set in the ad manager on Facebook the connections part is still unchanged.
'name' => 'Test Ad Set',
'billing_event' => 'APP_INSTALLS',
'optimization_goal' => 'APP_INSTALLS',
'campaign_status' => "PAUSED",
'daily_budget' => 5000,
'bid_amount' => 10,
'campaign_group_id' => $id,
'targeting' => json_encode($targeting),
$targeting = array(
'geo_locations' => array(
'countries' => array('US'),
),
'page_types' => array('mobilefeed'),
'user_os' => array('Android'),
'app_install_state' => 'NOT_INSTALLED',
);
Also tried moving the app_install_state outside of the targeting array to the adset level.
Incase you don't know what am talking about this is the part on the ad manager:

Bug with negative amounts in PayPal API

I'm trying to use a negative amount with the PayPal Adaptive Payments API. To start with, this seems to work:
'item' => [
{
'itemPrice' => '25.00',
'name' => 'Cryptex - 16Gb USB Drive',
'price' => '25',
'itemCount' => 1
},
{
'quantity' => 1,
'name' => 'Special Discount',
'price' => '-2.50'
},
{
'itemPrice' => '5.00',
'name' => 'Shipping',
'price' => '5.00',
'itemCount' => 1
}
]
Here is an example of what I see after submitting this as JSON, and updating the items:
As you can see, the price IS correct ... yet there is no sign of the discounted amount? Its obviously taking it into account - otherwise the price would have been £30, not £27.50 (which is after the £2.50 discount)
What gives? Is this a bug in PayPals system?
FWIW, the emails are also broken - they don't show the discount amount at all.
UPDATE: I've opened a ticket with PayPal tech support, as I'm pretty sure this is a bug with their system, and not something that can be fixed this end :( Damn annoying!
UPDATE 2: Reply from PayPal:
PayPal Adaptive Payments do not support negative item values. Since you did not pass the <itemPrice> in your API Call, the system did not throw the following error message:
[errorId] => 580022
[domain] => PLATFORM
[subdomain] => Application
[severity] => Error
[category] => Application
[message] => Invalid request parameter: itemPrice cannot be negative
I can see that the following was submitted to PayPal:
<item>
<name>Special Discount</name>
<identifier></identifier>
<price>-2.50</price>
<itemPrice></itemPrice>
<itemCount></itemCount>
<any>[quantity: null]</any>
</item>
This object is basically incomplete and in order to apply discounts, you should apply them > onto the price for the main item and then submit it to PayPal.
I'm really not sure what they're suggestion I do? An example cart, would be:
Item 1 - £10
Item 2 - £20
Item 3 - £50
Item 4 - £10
Delivery - £5
Total - £95
As an encouragement, we sometimes will offer coupons... so it could be "£10 off orders over £50". So in the above case above, it would drop the overall price to £85.
I'm really not sure what they are suggesting as an alternative?
Interesting. Normal payments don't have that restriction (I pass a negative amount item just like what you describe on a frequent basis).
What they're telling you to do is take the discount off another item. So your example cart with that discount would be
Item 1 - £10
Item 2 - £20
Item 3 - £40 <-- Removed £10 discount
Item 4 - £10
Delivery - £5
Total - £85 <-- total reflects this

HTML-Template - Array of Hash

I have two Array of Hashes: The first contains values for a current time interval and the second contains values for a previous time interval.
#AoHcurrent=
( { node => "ABC",
link => "DEF",
time => "10:00",
value => "100",
},
{
node => "FGH",
link => "IJK",
time => "10:00",
value => "200",
},
);
#AoHprevious=
( { node => "ABC",
link => "DEF",
time => "09:45",
value => "10",
},
{ node => "FGH",
link => "IJK",
time => "09:45",
value => "50",
},
);
I want to now use HTML-Template to present this data. Something like :
NODE LINK VALUE
---------------------
ABC DEF 100(10)
FGH IJK 200 (50)
the values in brackets represent the previous value.
my %html_template_parameters =
( AOHCURRENT => \#AoHcurrent,
AOHPREVIOUS => \#AoHprevious, );
my $html_template=qq{Report.tmpl};
my $html_output=qq{Report.html};
htmlReport($html_template,$html_output,\%html_template_parameters);
where htmlReport is a function that generates the report
I require guidance on defining the Report.tmpl file.
Thanks you in advance
see also http://www.perlmonks.org/?node_id=972954
I gave an example there how this can be solved with HTML::Template::Compiled.
Basically you would navigate through the parameter stash like this:
[%= expr=".AOHPREVIOUS[__index__]{'value'}" %]
or with the classic syntax:
<TMPL_VAR expr=".AOHPREVIOUS[__index__]{'value'}" >
You can't do that with 2 separate lists just with HTML::Template. And trying to do it with HTML::Template::Expr would be a nightmare to maintain. Try collapsing them into a single list where the hash data is merged.