segment_ga4: Make a segment object for use

View source: R/segments.R

segment_ga4R Documentation

Make a segment object for use

Description

A Segment is a subset of the Analytics data. For example, of the entire set of users, one Segment might be users from a particular country or city.

Usage

segment_ga4(
  name,
  segment_id = NULL,
  user_segment = NULL,
  session_segment = NULL
)

Arguments

name

The name of the segment for the reports.

segment_id

The segment ID of a built in or custom segment e.g. gaid::-3

user_segment

A list of segment_define's that apply to users

session_segment

A list of segment_define's that apply to sessions

Details

segment_ga4 is the top hierarchy of segment creation, for which you will also need:

  • segment_define : AND combination of segmentFilters

  • segment_vector_simple or segment_vector_sequence

  • segment_element that are combined in OR lists for ⁠segment_vectors_*⁠

Value

a segmentFilter object. You can pass a list of these to the request.

See Also

Other v4 segment functions: segment_define(), segment_element(), segment_vector_sequence(), segment_vector_simple()

Examples


## Not run: 
library(googleAnalyticsR)

## authenticate, 
## or use the RStudio Addin "Google API Auth" with analytics scopes set
ga_auth()

## get your accounts
account_list <- google_analytics_account_list()

## pick a profile with data to query

ga_id <- account_list[23,'viewId']


## make a segment element
se <- segment_element("sessions", 
                      operator = "GREATER_THAN", 
                      type = "METRIC", 
                      comparisonValue = 1, 
                      scope = "USER")
                      
                      
se2 <- segment_element("medium", 
                       operator = "EXACT", 
                       type = "DIMENSION", 
                       expressions = "organic")
                       
                       
## choose between segment_vector_simple or segment_vector_sequence
## Elements can be combined into clauses, which can then be 
##    combined into OR filter clauses
sv_simple <- segment_vector_simple(list(list(se)))

sv_simple2 <- segment_vector_simple(list(list(se2)))

## Each segment vector can then be combined into a logical AND

seg_defined <- segment_define(list(sv_simple, sv_simple2))

## if only one AND definition, you can leave out wrapper list()

seg_defined_one <- segment_define(sv_simple)

## Each segement defintion can apply to users, sessions or both.
## You can pass a list of several segments

segment4 <- segment_ga4("simple", user_segment = seg_defined)
## Add the segments to the segments param

segment_example <- google_analytics(ga_id, 
                                    c("2015-07-30","2015-10-01"), 
                                    dimensions=c('source','medium','segment'), 
                                    segments = segment4, 
                                    metrics = c('sessions','bounces')
                                     )
                                     
                                     
## Sequence segment

se2 <- segment_element("medium", 
                       operator = "EXACT", 
                       type = "DIMENSION", 
                       expressions = "organic")
                       
se3 <- segment_element("medium",
                       operator = "EXACT",
                       type = "DIMENSION",
                       not = TRUE,
                      expressions = "organic")
                      
## step sequence
## users who arrived via organic then via referral
sv_sequence <- segment_vector_sequence(list(list(se2), 
                                            list(se3)))
                                            
seq_defined2 <- segment_define(list(sv_sequence))

segment4_seq <- segment_ga4("sequence", user_segment = seq_defined2)

## Add the segments to the segments param

segment_seq_example <- google_analytics(ga_id, 
                                        c("2016-04-01","2016-05-01"), 
                                        dimensions=c('source','segment'), 
                                        segments = segment4_seq,
                                        metrics = c('sessions','bounces')
                                         )
                                     

## End(Not run)


googleAnalyticsR documentation built on Sept. 11, 2024, 8:15 p.m.