GA4 (Google Analytics) sessions based on UTM params - google-analytics-api

I am trying to fetch sessions from GA4 which are relevant to specific UTM params.
In GA3 we were able to use segments (sessions::condition::ga:source==X;ga:medium==Y) but I can not find a way to do this on GA4.
POST https://analyticsdata.googleapis.com/v1beta/#{property}:runReport`
Payload like this:
body = {
"metrics": [
{
"name": "sessions::condition::ga:source==X;ga:medium==Y"
}
],
"dimensions": [
{
"name": "date"
}
],
"dateRanges": [
{
"startDate": '2022-01-01',
"endDate": '2022-01-30',
"name": "current_year"
}
]
}
Returns: Field sessions::condition::ga:source==X;ga:medium==Y is not a valid metric.. Is there a way to do this via new API?
Should I use dimension filter to achieve that? I need to query on both source&medium but it is not clear how do I do this?
"dimensionFilter": {
"filter": {
"fieldName": "firstUserMedium",
"stringFilter": {
"value": "Y"
}
}
}

A dimension filter on sessionSource & sessionMedium returns sessions that have those specific utm_source & utm_medium values. See the dimensions & metrics page for a description of these and other dimensions & metrics.
The needed dimension filter is similar to the following. See Dimension Filters in Creating a Report for more info.
"dimensionFilter": {
"andGroup": {
"expressions": [
{
"filter": {
"fieldName": "sessionSource",
"stringFilter": {
"value": "X"
}
}
},
{
"filter": {
"fieldName": "sessionMedium",
"stringFilter": {
"value": "Y"
}
}
}
]
}
},
Segments are not yet available today in the GA4 Data API.

I think you should check the dimensions and metrcis list for GA4 they dont start with ga
POST https://analyticsdata.googleapis.com/v1beta/properties/GA4_PROPERTY_ID:runReport
{
"dateRanges": [{ "startDate": "2020-09-01", "endDate": "2020-09-15" }],
"dimensions": [{ "name": "country" }],
"metrics": [{ "name": "activeUsers" }]
}
Also at this time i don't think it supports segments.

Related

Loopback 3 query by Property of a embedded model

I'm using loopback 3 to build a backend with mongoDB.
So i have 2 models: Object and Attachment. Object have a relation Embeds2Many to Attachment.
Objects look like that in mongoDB
[
{
"fieldA": "valueA1",
"attachments": [
{
"id": 1,
"url": "abc.com/image1"
},
{
"id": 2,
"url": "abc.com/image2"
}
]
},
{
"fieldA": "valueA2",
"attachments": [
{
"id": 4,
"url": "abc.com/image4"
},
{
"id": 5,
"url": "abc.com/image5"
}
]
}
]
The question is: how can i get Objects with attachments.id=4 over the RestAPI?
I have try with the where and include filter. But it didn't work. It look like, that this function is not implemented in loopback3, right?
I have found the solution. It only works on Mongodb, Cloudant and Memory database.
{
"filter": {
"where": {
"attachments.id": 4
}
}
}

Handle multiple answers from IBM Cloud Function in Watson Assistant

I need to show an unknown number of buttons in a Watson Assistant dialogue node. The data for the buttons comes from a IBM Cloud Function.
If I manually set up a response type "option" answer in my node the JSON-object looks like this:
{
"output": {
"generic": [
{
"title": "Välj mötestyp",
"options": [
{
"label": "Rådgivning familjerätt 30 min",
"value": {
"input": {
"text": "447472"
}
}
},
{
"label": "Rådgivning familjerätt 60 min",
"value": {
"input": {
"text": "448032"
}
}
}
],
"description": "Välj typ av möte du vill boka",
"response_type": "option",
"preference": "dropdown"
}
]
}
}
My cloud function can create this JSON with x no of options. But how can I use this data in Assistant?
The easiest would be to let the cloud function generate the complete JSON and then just output the returned JSON like this:
{
$context.output"
}
..but that's not allowed.
Generated output-object from my function:
[{"serviceId":447472,"serviceName":"Rådgivning Familjerätt 30 min"},{"serviceId":448032,"serviceName":"Rådgivning Familjerätt 60 min"}]
Any advice on how to do this?
I don't see a simple way of generating the entire output and options. What you could do is this:
Generate the option labels and values
Pass them into a generic output node that has predefined structures for 1, 2, 3, etc. options. Check based on the array size of your context variable which predefined response structure to fill.
I tested the following:
{
"context": {"my": [ {
"label": "First option",
"value": "one"
},
{
"label": "Second",
"value": "two" }]},
"output": {
"generic": [
{
"title": "This is a test",
"options": [{"label": "<? $my[0].label ?>",
"value": {
"input": {
"text": "my[0].value"
}
}
},{"label": "<? $my[1].label ?>", "value": {
"input": {
"text": "<? $my[1].value ?>"
}
}
}],
"response_type": "option"
}
]
}
}
It defined a context variable with the options, analogous to the options structure. In the output access the labels and values, later modifying the to proove that they are used and can be modified.

Why `time_range` doesn't work in request to Facebook API?

I try to get some data from Facebook Marketing API. The result I get are generally correct but I have problem with data. It seems like parametr time_range doesn't work.
https://graph.facebook.com/v2.7/act_<MY_ACT>/ads?fields=name,adcreatives{object_story_id},insights{spend,unique_clicks},effective_status&params=time_range={%22since%22:%222016-11-15%22,%22until%22:%222016-11-17%22}&limit=2000&access_token=<MY_TOKEN>
Result:
{
"data": [
{
"name": "xx",
"adcreatives": {
"data": [
{
"id": "xx"
}
]
},
"effective_status": "DISAPPROVED",
"id": "xx"
},
{
"name": "xx",
"adcreatives": {
"data": [
{
"id": "xx"
}
]
},
"insights": {
"data": [
{
"spend": xx,
"unique_clicks": "34",
"date_start": "2016-10-19",
"date_stop": "2016-11-17"
}
],
"paging": {
"cursors": {
"before": "MAZDZD",
"after": "MAZDZD"
}
}
},
"effective_status": "CAMPAIGN_PAUSED",
"id": "xx"
},...}
I tried encode it, write in different way, but each time the results are from the last month, so date_start is 2016-10-19 and date_stop is 2016-10-17, not from range I put as parametr in query. How can I fix it?
Edit:
I also tried instead of &params=time_range={%22since%22:%222016-11-15%22,%22until%22:%222016-11-17%22} something like &date_preset=yesterday (yesterday or other correct value listed in documentation) but still I get exact same dates...
You can use the following to get insights within certain time_range:
/act_<ACCT_ID>/insights?level=ad&time_range={"since":"2016-11-15","until":"2016-11-17"}

Country dimension in Analytics Reporting API v4

I want to get a report from Google Analytics v4 for pageviews by country. Since my website is targeting a specific region, I'm filtering by countries I'm most interested in using ga:countryIsoCode dimension so I don't have to type complete country name in the filter.
Now I'd be also interested in getting pageviews for all other countries combined into 'other' entry. What would be the easiest way to achieve that?
I've tried dynamic segments so far adding ga:segment to dimensions and a NOT filter in the segment with the same rule I used for filtering counties (except ga:countryIsoCode isn't allowed in segments, so I used ga:country). This produced 3 rows of data with each of three countries appearing along with 'other' every time.
I'd appreciate an advice on this as it seems like a common use case to me. Thanks in advance!
I was able to achieve that by using segments. This approach doesn't allow to batch requests though unless all of them have the same segment definition. Also, there're limitations on the number of segments to include in the report request. Very surprised this functionality is so hard to achieve. For interested, there's an example request:
{
"reportRequests": [{
"viewId": "ga:XXXXXXXXX",
"dateRanges": [{
"startDate": "YYYY-MM-DD",
"endDate": "YYYY-MM-DD"
}],
"metrics": [{
"expression": "ga:pageviews"
}, {
"expression": "ga:sessions"
}],
"dimensions": [{
"name": "ga:segment"
}],
"segments": [{
"dynamicSegment": {
"name": "Other",
"sessionSegment": {
"segmentFilters": [{
"not": "True",
"simpleSegment": {
"orFiltersForSegment": [{
"segmentFilterClauses": [{
"dimensionFilter": {
"dimensionName": "ga:country",
"operator": "IN_LIST",
"expressions": ["United States", "United Kingdom"]
}
}]
}]
}
}]
}
}
},
{
"dynamicSegment": {
"name": "United States",
"sessionSegment": {
"segmentFilters": [{
"simpleSegment": {
"orFiltersForSegment": [{
"segmentFilterClauses": [{
"dimensionFilter": {
"dimensionName": "ga:country",
"operator": "EXACT",
"expressions": "United States"
}
}]
}]
}
}]
}
}
},
{
"dynamicSegment": {
"name": "United Kingdom",
"sessionSegment": {
"segmentFilters": [{
"simpleSegment": {
"orFiltersForSegment": [{
"segmentFilterClauses": [{
"dimensionFilter": {
"dimensionName": "ga:country",
"operator": "EXACT",
"expressions": "United Kingdom"
}
}]
}]
}
}]
}
}
}],
'hideTotals': 'true',
'hideValueRanges': 'true'
}]
}

Magento 2 REST API - get product by slug, or get media in REST search

I have an ember app which I'm using as the front end. I need to fetch a product from the REST api but instead of using the SKU, I need to use the slug. So I access the following endpoint which works fine: http://*.com/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=url_key&searchCriteria[filter_groups][0][filters][0][value]=daniels-icecream-slug
However, the result is obviously a product list as opposed to the product endpoint, so some of the data is omitted. Namely, the media_gallery_entries field. So is there anyways I can either return this data in the /products?searchCriteria endpoint or is there a way I can fetch /products/:slug instead of /products/:sku for the product endpoint?
you need to define conditionType as well with the API Call like following
V1/products/?searchCriteria[filterGroups][0][filters][0][field]=url_key&searchCriteria[filterGroups][0][filters][0][value]=%shirt%&searchCriteria[filterGroups][0][filters][0][condition_type]=like
Parameters :
searchCriteria[filterGroups][0][filters][0][field]=url_key
searchCriteria[filterGroups][0][filters][0][value]=%shirt%
searchCriteria[filterGroups][0][filters][0][condition_type]=like
Note: Make sure to prefix & suffix % in value as per your requirements.
I am using the same in my api Calls and it works
I'm using Magento v2.2, and when I do a search, each item has a image attribute (in the custom_attributes list) that mangento automatically adds to a product when up add an image to it:
{
"items": [{
"sku": "MH07-XS-Black",
"name": "Hero Hoodie-XS-Black",
"custom_attributes": [{
"attribute_code": "description",
"value": "<p>Gray and black color blocking sets you apart as the Hero Hoodie keeps you warm on the bus, campus or cold mean streets. Slanted outsize front pockets keep your style real . . . convenient.</p>\n<p>• Full-zip gray and black hoodie.<br />• Ribbed hem.<br />• Standard fit.<br />• Drawcord hood cinch.<br />• Water-resistant coating.</p>"
},
{
"attribute_code": "image",
"value": "/m/h/mh07-black_main.jpg"
},
{
"attribute_code": "small_image",
"value": "/m/h/mh07-black_main.jpg"
},
{
"attribute_code": "thumbnail",
"value": "/m/h/mh07-black_main.jpg"
},
{
"attribute_code": "color",
"value": "49"
},
{
"attribute_code": "minimal_price",
"value": "54.0000"
},
{
"attribute_code": "category_ids",
"value": [
"15"
]
},
{
"attribute_code": "options_container",
"value": "container2"
},
{
"attribute_code": "required_options",
"value": "0"
},
{
"attribute_code": "has_options",
"value": "0"
},
{
"attribute_code": "url_key",
"value": "hero-hoodie-xs-black"
},
{
"attribute_code": "msrp_display_actual_price_type",
"value": "0"
},
{
"attribute_code": "tax_class_id",
"value": "2"
},
{
"attribute_code": "size",
"value": "167"
}
]
}]
}