Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segments doesn't work for GA4 #382

Open
benAnthropics opened this issue Apr 29, 2021 · 7 comments
Open

Segments doesn't work for GA4 #382

benAnthropics opened this issue Apr 29, 2021 · 7 comments
Assignees
Labels

Comments

@benAnthropics
Copy link

benAnthropics commented Apr 29, 2021

I can't make segments work for GA4, I use the code below from another comment and it says

"Error in ga_data(my_property_id, date_range = c("2020-07-23", Sys.Date() - :
unused argument (segments = seg)"

When I change it to google_analytics it returns an error stating user doesn't have permission. Having looked at the documentation for the GA API segements doesn't seem to be supported, so did this ever work? Could someone share a working example if so?

thanks,

Ben

se <- segment_element(name = "dimension21", operator = "REGEXP", type = "DIMENSION", expressions = "New")
sv <- segment_vector_simple(list(list(se)))
sd <- segment_define(list(sv))
seg <- segment_ga4("new_version", user_segment = sd)

count <- ga_data(my_property_id,
                          date_range = c("2020-07-23", Sys.Date()-1),
                          metrics = c("ga:uniquePageviews"),
                          dimensions = c("date","ga:pagePath"),
                          segments =  seg,)
@MarkEdmondson1234
Copy link
Collaborator

Yes segments don't exist for GA4, they are a session based concept which doesn't fit the new event data model. Use filters instead.

@MarkEdmondson1234 MarkEdmondson1234 changed the title Segements doesn't work for GA4 Segments doesn't work for GA4 Apr 29, 2021
@lucasjohnston
Copy link

Hi @MarkEdmondson1234 – I understand the difference between sessions and events, but you can still create segments at the session-level in the GA4 UI. I understand filtering can be done but we can't reach the same level of data that way. Is this a GA4 API restriction or a decision for the project?

@MarkEdmondson1234
Copy link
Collaborator

2 years ago there weren't segments in the API, but now they are in there as Audiences and funnels. However I'm committed to other projects so not looking at this myself for the foreseeable future but welcome any pull requests.

@DamonDay
Copy link

2 years ago there weren't segments in the API, but now they are in there as Audiences and funnels. However I'm committed to other projects so not looking at this myself for the foreseeable future but welcome any pull requests.

Hello Mark, I greatly appreciate your contribution to the development of the GA API for R. Your package is my top weapon in my arsenal. I would like to promote this project, but I have limited experience in developing R packages. Could you please guide me on which raw file to access in order to integrate audiences and funnels into this package?

@MarkEdmondson1234
Copy link
Collaborator

I suggest initially get the API calls to work via ga_data(raw_json=list(blah blah)) - so you can get comfortable with what json objects need to be sent in to get the responses you want. This json can be generated via the GA4 API explorer such as here: https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/properties/runFunnelReport

In the APIs alpha channel are the new endpoints:

As its alpha, set the API version at the top of the ga_data.R script:

version_aw <- function(){
  "v1alpha" # chage in your local version
}

Once you have it working with raw_data then create an R function that will generate the JSON you know works. Make that function as user friendly as possible :)

Then the output of that function (which should be I guess an R list that will be turned into JSON in the request) should be invoked by a parameter in the original ga_data() function (e.g. something like ga_data(..., funnels=ga_funnel(blah blah) and the json output added to the body sent into the API request:

  brrr <- RunReportRequest(
        metrics = mets,
        dimensions = dims,
        dateRanges = dates,
        limit = limit,
        dimensionFilter = dimensionFilter,
        metricFilter = metricFilter,
        metricAggregations = metricAggregations,
        orderBys = orderBys,
        keepEmptyRows = TRUE,
        returnPropertyQuota = TRUE,
        # add new JSON objects here

      )
  
  ga_aw_report(propertyId, brrr, page_size)

@DamonDay
Copy link

I suggest initially get the API calls to work via ga_data(raw_json=list(blah blah)) - so you can get comfortable with what json objects need to be sent in to get the responses you want. This json can be generated via the GA4 API explorer such as here: https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/properties/runFunnelReport

In the APIs alpha channel are the new endpoints:

As its alpha, set the API version at the top of the ga_data.R script:

version_aw <- function(){
  "v1alpha" # chage in your local version
}

Once you have it working with raw_data then create an R function that will generate the JSON you know works. Make that function as user friendly as possible :)

Then the output of that function (which should be I guess an R list that will be turned into JSON in the request) should be invoked by a parameter in the original ga_data() function (e.g. something like ga_data(..., funnels=ga_funnel(blah blah) and the json output added to the body sent into the API request:

  brrr <- RunReportRequest(
        metrics = mets,
        dimensions = dims,
        dateRanges = dates,
        limit = limit,
        dimensionFilter = dimensionFilter,
        metricFilter = metricFilter,
        metricAggregations = metricAggregations,
        orderBys = orderBys,
        keepEmptyRows = TRUE,
        returnPropertyQuota = TRUE,
        # add new JSON objects here

      )
  
  ga_aw_report(propertyId, brrr, page_size)

Thanks Mark, I think I can get it by the end of this year. :)

@meeribad
Copy link

meeribad commented May 6, 2024

Hello, Mark and 8 bit sheep ,

I am aware that this project has been transferred but is there any way segments can be brough to the R package please?

I did some digging around but its too above my current level of expertise to understand any of these things and i have repeatedly requested google devs for support but there has been no response from them to add segments to runreport method in data api.

The segments are available in the alpha funnel report : https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/properties/runFunnelReport#segment

Waiting for this feature to be added as its impossible to create AB test reports without segments and automate them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants