How to individually segment metrics in one report in Google Analytics? - google-analytics

In Google Analytics or Google Analytics API App script.
How can I view/pull data from 2 metrics are segmented on different dimensions?
ie
Dimension:page
Metric 1 :users segmented on demographic:gender:male
Metric 2 :users segmented on demographic:gender:female
ie How would I combine
var resultsM = Analytics.Data.Ga.get(
profileView, //view id
dataStartDate,
dataEndDate,
'ga:users', //metrics
optArgsMale
);
var resultsF = Analytics.Data.Ga.get(
profileView, //view id
dataStartDate,
dataEndDate,
'ga:users', //metrics
optArgsFemale
);

For this king of query I would use the filter property which support regex.
Typically if you want to pull data from the sources xxx or yyy, you would add to your query:
filter: "ga:source=~xxx|yyy"
The "=~" will tell the api you are passing a regex rather than an exact match ("==").

Related

How to include empty rows data in Google Analytics Reporting API (V3)?

Today I have added another custom dimension in the google analytics- its new custom dimension (dimension 5)
let gaData: any = {
'start-date': st_date,
'end-date': end_date,
'ids': "a:myviewid", //
'metrics': 'ga:pageviews,ga:timeOnPage,ga:sessions',
'dimensions': "ga:dimension1,ga:dimension5",
'start-index': 1 + (this.pageNo - 1) * this.pageSize,
'max-results': this.pageSize
}
Using Google V3 Get API, after adding query params to the URL the final URL will become as
https://content.googleapis.com/analytics/v3/data/ga?start-date=2021-04-26&end-date=2021-05-26&ids=ga:myviewid&metrics=ga:pageviews,ga:timeOnPage,ga:sessions&dimensions=ga:dimension1,ga:dimension5&start-index=1&max-results=10
As of today, I added a:dimension5 so it's not giving any data. When I remove dimension 5 its giving correct number of users' data.
I have checked include-empty-rows - but in the v3 api its already by default as true - Refer https://developers.google.com/analytics/devguides/reporting/core/v3/reference#includeEmptyRows
So if there is no dimension5 data at least I should get the data, otherwise, there is a huge data loss.
Assuming if I add another dimension after 1 year then 1 year data will not come which will be very annoying.
Any suggestion will be of great help.
Thanks in advance!
You have to add some parameter like this,
include_empty_rows=True
include_empty_rows have default parameter as null,
For more information, see this page.
https://developers.google.com/resources/api-libraries/documentation/analytics/v3/python/latest/analytics_v3.data.ga.html

Google Analytics API: Report dimensions and metrics are null

I am using the sample code from https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php, and printResults is failing because $dimensionHeaders and $dimensions are null
Also, it seems to only work if "ga:" is prepended to the view ID provided by google analytics admin\view\view settings\view ID. Is that correct, or am I using the wrong view ID?
Yes, the correct View ID is what the Google Analytics admin interface is in:
Admin --> View --> View settings --> View ID
Like this:
Google's sample code is missing this key bit in getReport() underneath the line: $request = new Google_Service_AnalyticsReporting_ReportRequest();
//Create the Dimensions object.
$browser = new Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:browser");
$request->setDimensions(array($browser));

Google Analytics dimension pagePathLevel more than 4

I have very long web pages paths reported to google analytics:
/#/legends_g01/games/legends_g01_02_academy_i-170909-55/notes/1/dynamics
/#/legends_02_academy_i/games/legends_g01_02_academy_i-170912-64/notes/12/players
/#/legends_05/games/legends_05-170912-84/notes/22/players
/#/legends_g01_02_academy_i/games/legends_g01_02_academy_i-170919-78/notes/34/levels
I'm using Core API to create a query where I need to have a metric ga:users with dimension by the last path part (7th). The starting part of the path doesn't matter here and should be ignored.
So if there is ga:pagePathLevel7 then I can use
dimension: ga:pagePathLevel7
metrics: ga:users
And see the result like this:
dynamics: 34
players: 45
levels: 87
How can I do this without ga:pagePathLevel7?
It seems that I'm the only one here with such a problem.
As I failed to find a direct solution I ended up adding custom dimensions to my google analytics. I added the dimensions for the last important path parts and changed the code on the site to supply this data together with the pageView url.
import ReactGA from 'react-ga';
export const statDimension = (dimensionName, value) => {
if(value)
{
let obj = {};
obj[dimensionName] = value;
ReactGA.set(obj);
}
};
export const statPageView = (url, game_id, clip_num) => {
if(!url)
{
url = window.location.hash;
}
//set game_id
statDimension(STAT_DIM_GAME_ID, game_id);
//set clip number
statDimension(STAT_DIM_CLIP_NUM, clip_num);
ReactGA.pageview(url);
return null;
};
I use react-ga npm module for submitting data to google analytics.
Now I can use custom dimensions together with filters on my urls to get stats based on the parts of the path with depth > 4.
May be that's not an elegant solution but is a working one.
Hope this will be helpful for somebody like me.

How to get statistics of where visitors come from via URL params

There are many QR codes that contains URL of website such as:(it just demos link)
http://www.popupstore.com/index.php?qrcode_type=magazine&location=Singapore
http://www.popupstore.com/index.php?qrcode_type=banner&location=Vietnam
I need a way can summary to know that where customer come from (nearly same as source/channel in Google Analytics):
Type: Mazazine, banner, etc.
Location: Vietnam, Singapore, etc.
Can anyone help me please :)
You could create two Custom Dimensions, each for Type and another for Country
As per your need define the appropriate Scope of the dimension, a Hit level or Session level scope would be appropriate.
You need to push custom dimensions into Google Analytics i.e. additonal JS code in your site.
ga('send', 'pageview', {
'dimension1': 'Magzine',
'dimension2': 'Singapore'
});
How this works
User scans the code and visits the store
Site has a JS code snippet that would get the query parameters from the URL and sets a custom dimension for each parameter
Setting the custom dimension would let Google Analytics know the value of the Type and Country
It is your JS code that tells Google Analytics what value to take for custom dimension. Google Analytics would not know that the value came from the URL.
To get a query parameter value via javascript you can refer to this answer, If you take the function provided there by Jan Turon (head over and give him an upvote of this helps you):
function getJsonFromUrl() {
var query = location.search.substr(1);
var result = {};
query.split("&").forEach(function(part) {
var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
return result;
}
You can use this to dynamically set the dimensions based on the url. You first call the function to return an JSON object that has the key/value pairs from the query parameters, then you insert the needed values to set the dimensions:
result = getJsonFromUrl();
ga('send', 'pageview', {
'dimension1': result.qrcode_type,
'dimension2': result.location
});

Individual User ID with Universal Analytics

I am attempting to implement a very basic UserID parameter, so that I can more easily slice the data we are receiving (ie attribute User to device, returning/non-returning, location, source, event)
I am using this at the moment to create unique IDs for each visit:
var timedate = new Date();
var timedatemil = timedate.getTime();
ga('create', 'UA-XXXXXXXXX', { 'userId': timedatemil});
To produce a simple ID for each visit. However I am not receiving any data this way when I switch to the userID view. Why would this be? I am running a simple landing page so have no identifiable system to tie it to currently.

Resources