Google Analytics Data API (GA4) - item.variant dimension? - google-analytics

So, ga4-ecommerce has the property "variant" for it's items:
(see https://developers.google.com/tag-manager/ecommerce-ga4)
dataLayer.push({
event: "view_item_list",
ecommerce: {
items: [
{
item_name: "Triblend Android T-Shirt", // Name or ID is required.
item_id: "12345",
price: 15.25,
item_brand: "Google",
item_category: "Apparel",
item_category2: "Mens",
item_category3: "Shirts",
item_category4: "Tshirts",
item_variant: "Gray",
item_list_name: "Search Results",
item_list_id: "SR123",
index: 1,
quantity: 1
},
});
But somehow I don't seem to find the dimension for item.variant. Pretty much every other prop is represented by its own dimension in the API-Schema:
https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema#dimensions
Is this by design and I can somehow retrieve aggregated metrics based on variant?
f.e. I want to retrieve aggregated data to see how much itemViews (metric) each itemVariant (the dimension I'm missing?) generated.

There is not a way to retrieve the itemVariant dimension in the GA4 Data API. The dimensions from the item array are prefixed with the string item in the API Dimensions & Metrics list.
The GA4 Data API presents most of the dimensions available in the GA4 UI; an Item Variant dimension is also not available in the GA4 UI.

I was setting up a new build and decided the best way to keep this was to drop "category 4" and use that instead. Understand this won't work for build's with historical data using the category 4/5 for their intended purposes!

Related

Analytics E-commerce items storing all data but not revenue data

I work on a website that sells some online courses and I prepared everything to send data to google analytics via GTM, but I've been having a problem recently that I can't find a solution.
On every e-commerce related event present on the website. The "items" information is being sent (since I can see which items are the most sold). But Analytics apparently isn't receiving the price information from each item since on my reports the Revenue for each item is 0. But the revenue for the purchases in general is being received correctly, the problem is only on the item level.
These are the parameters being sent to event as seen on our GTM:
[
{name: "currency", value: "EUR"},
{
name: "items",
value: [
{
item_name: "equivalencia-medica-Ginecologia",
price: "64.99",
quantity: "1",
currency: "EUR"
}
]
},
{name: "value", value: "64.99"}
]
When I use Analytics DebugView and try to add an item to the cart, I do receive the add_to_cart event, but it only shows the "currency" and "value" parameters. The "items" parameter is nowhere to be found.
Any ideas why every possible data is being stored but not the price of the items? Please comment if more code needed.
try removing quotes price: 64.99 . Try following documentation

How to stop re-orderring of the array elements when updating array elements in firebase

I am actually trying to change the quantity of the cart item , and every time i change , the whole product gets re-ordered by itself,
I am not able to figure out based on what the elements are being re-ordered.
How to stop this from happening?
Before Updating the quantity the cart items are as follows
After updating the quantity the cart items are as follows, The product gets reordered automatically.
My code for updating is :
void setQuantity(CartItemModel item, int quantity) {
removeCartItem(item);
item.quantity = quantity;
item.cost = (int.parse(item.price!) * item.quantity!).toString();
utilityController.updateUserData({
'cart': FieldValue.arrayUnion([item.toJson()])
});
}
It's because the array element is deleted and then added ,
But how to overcome this problem !!
Firestore "arrays" are ABSOLUTELY NOT ARRAYS - they are "ordered lists" - the "number" is their order, not an index. The ORDER of the entries in the object are important, as well, so:
{
id: "xxxxxx",
img: "xxxxx",
name: "xxxxx",
nameKa: "xxxxx"
}
WILL NOT MATCH
{
id: "xxxxxx",
name: "xxxxxx",
nameKa: "xxxxxx",
img: "xxxxxx",
}
Firestore's scale and speed come from indexing entries, and an "array" (ordered list) of objects is essentially indexed by a string-like representation of the object.
Firestore "arrays" (ordered lists) of objects are remarkably difficult to use, and give you no advantages - they are much better suited to "single value" entries. I would strongly recommend using a sub-collection of documents (each member in it's own document), where you can trivially query (either a a collection or collectionGroup) to find individual documents.

How to insert data in to datatstore

How to insert data in to datastore?
The data could be like the one below:
{
'food': [{
"item_name": item,
'price': price
}, {
"item_name": item,
'price': price
}],
'beverages': [{
''
'beverage_name': beverage,
'beverage_price': b_price
}, {
''
'beverage_name': beverage,
'beverage_price': b_price
}]
}
The data that you are trying to add to the Google Cloud Datastore is a JSON string. The way you have it in your question is wrong structured. The proper JSON example would be:
{
"food": [
{ "food_name":"NAME1", "food_price":"PRICE1" },
{ "food_name":"NAME2", "food_price":"PRICE2" },
{ "food_name":"NAME3", "food_price":"PRICE3" }
],
"beverages":[
{ "beverage_name":"NAME1", "beverage_price":"PRICE1" },
{ "beverage_name":"NAME2", "beverage_price":"PRICE2" }
]
}
To add the data from the JSON string to the Datastore you have to:
Load the JSON string as JSON object to be able to go through its fields
Create a client to access Google Datastore
Set the key food for the Kind value in Datastore
Use the entity to add the data to the Datastore
Set the key beverages for the Kind value in Datastore
Use again entity to add the data to the Datastore
For further information, you can refer to Google Cloud Data Store Entities, Properties, and Keys documentation.
I have done a little bit coding myself and here is my code example in GitHub for Python. You can take the idea of how it works and test it. It will create two different Kind values in Datastore and add the food data in foods and beverage data to beverages.

Advanced multiple search query in Firebase

This is my Firebase database inside "/articles", which has loads of articles inside. A user can (using his/her own article), list other articles that correspond to certain conditions. In order for a article to pass the query test, it has to be of category that the user's article has listed inside "tradableCategories", while also THAT article needs to have the user's article's category within its "tradableCategories".
Here’s the database structure:
"articles": {
"article1": {
"title": "Car",
"category": "vehicles",
"owner": "user1",
"tradableCategories": {
"furnishings": true,
"other": true,
"vehicles": true
},
"category_tradableCategories": {
"vehicles_furnishings": true,
"vehicles_other": true,
"vehicles_vehicles": true
}
},
"article2": {
"title": "Bike",
"category": "vehicles",
"owner": "user2",
"tradableCategories": {
"furnishings": true,
"other": true
"vehicles": true,
},
"category_tradableCategories": {
"vehicles_furnishings": true,
"vehicles_other": true,
"vehicles_vehicles": true
}
},
"article2": {
"title": "Couch",
"category": "furnishings",
"owner": "user2",
"tradableCategories": {
"furnishings": true,
"other": true,
"vehicles": true
},
"category_tradableCategories": {
"furnishings_furnishings": true,
"furnishings_other": true,
"furnishings_vehicles": true
}
},
...
}
user1 owns article1, which wants to find articles that are within furnishings, other and vehicles. Those articles that match the conditions also have to look for article1’s set category. The query can be done easily using SQL:
SELECT *
FROM articles
WHERE category = ’vehicles’ /* This is article1’s category */
AND find_in_set(category, :tradableCategories) /* :tradableCategories is a stringified, comma-separated set of article1’s tradableCategories: “furnishings,other,vehicles” */
AND NOT owner = ‘user1’
As you’ve seen in the database structure. I have included another object called “category_tradableCategories”. I’ve seen various answers here on Stack Overflow that explain how to search for items using two conditions combined into one. This could’ve worked but means that I have to initiate 3 Firebase queries since I cannot combine three (or more) different categories within tradableCategories.
I am afraid this is too complicated for Firebase, but if there is any efficient solution to this I’d like some help. Thank you!
In relational databases you often first define your data model to match with the data you want to store and then write queries for the use-cases of your app. In NoSQL databases you typically use the inverse logic: you make a list of your app's use-cases and then define your data model to match those.
If Firebase's API doesn't directly support the query you want to build, you'll typically have to change/augment your data model to allow that query. This will lead to storing more data and more complex updates, but the advantage is that you have faster and simpler read operations.
So in your scenario: you want a list of articles in one of three categories that is not owned by the current user. The most direct mapping of that requirement would be to literally store that list:
user_articles
$uid
categories_1_2_3
articlekey1: true
articlekey2: true
This would make the query trivial: ref.child("user_articles").child(currentUser.uid).child(categories).on("child_added"....
Now this may be taking the denormalization and duplication a bit too far. We'd need a separate list for each user/category combination. So an article in 3 categories with 10 users would end up in 60 lists.
More likely you'll want to keep these articles-per-categories in a single list across all users. For example:
articles_by_category_with_owner
category_1
articlekey1: uid1
articlekey2: uid2
articlekey3: uid1
category_2
articlekey1: uid1
articlekey2: uid2
category_3
articlekey1: uid1
articlekey3: uid1
Now you can get all article keys with category_1 with ref.child("articles_by_category_with_owner").child(category).on("child_added"... and then do the "not owned by the current user" filtering client-side.
In the above list I've also removed the multiple-categories. That does mean that you'll need to read a node for each category. But this is actually not as slow as you may expect, since Firebase pipelines these requests (see link below).
Further recommended reading/viewing:
NoSQL data modeling
Firebase for SQL developers
Questions/answers from this list
Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly
Query based on multiple where clauses in firebase

How can I achieve this Many-to-many relationship in Firebase?

I know Firebase does not support JOINs between nodes (like SQL Server does between tables), but that is exactly what I need to accomplish. Here's my situation:
I have a transactions node in Firebase like this (where I am including the category name for each transaction):
"transactions":
{
"-Jruazf35b9a_gAVmZBe":
{
payee: "McDonalds", amount: "2.35", category: "Eating Out"
}
"-JruadR11b4a_aTVmZFi":
{
payee: "Walmart", amount: "78.12", category: "Household"
}
"-Jruazf35b9a_AgvNWCq":
{
payee: "CapitalOne", amount: "150.00", category: "Debt"
}
"-JryJF2c33ijbjbBc24p":
{
payee: "FootLocker", amount: "107.54", category: "Personal Blow"
}
"-Jrz0T-aL61Vuw4SOqRb":
{
payee: "Starbucks", amount: "2.88", category: "Eating Out"
}
}
And I have a Categories node like this (where I am including the transactions under each category):
"categories":
{
"-Jruazf35b2a_gAVmZRy":
{
categoryname: "Eating Out",
categorytype: "Expense"
}
"transactions": {
"-Jruazf35b9a_AgvNWCq": {
payee: "McDonalds", amount: "2.35"
}
.
.
.
}
}
}
So far so good. My data is flat. I'm able to show the list of transactions with the category name (screenshot below) and I can show the list of transactions under each category in the expenses per category section (screenshot not shown here).
The problem I have is that if I rename a category the change is only reflected for future transactions. Past transactions show the old category name.
This is obvious due to the way I'm saving the data. So my first logical reaction was to save the category unique ID in the transactions node instead of the category name. However, that presents the challenge where, in my SQL Server little brain, I would need a JOIN so I can get the list of transactions and also include the name of the category for each transaction.
How can I structure my data so that I can:
show a list of transactions including the name of the category (as it does today)
allow a user to rename a category and show the change reflected for ALL transactions (past and future)
show a list of transactions under each category (I think the current approach would still be valid)
Joining data from two lists is inherently a slow operation, especially on NoSQL databases.
I would recommend keeping the categoryName and adding a categoryId. That way you can show your current screen with a single read, but also link to the category. For how to deal with updating the categoryName in each transaction, see How to write denormalized data in Firebase and https://medium.com/#collardeau/es6-promises-with-firebase-76606f36c80c.
Alternatively: the list of categories is likely to be relatively small. So you could also pre-load it and perform a client-side lookup while you're iterating the transactions.

Resources