Coder Social home page Coder Social logo

Comments (6)

MarkEdmondson1234 avatar MarkEdmondson1234 commented on August 25, 2024

Nice, so the idea is the same expressions will work in v3 or v4 ? I see you have gone through the objects, they should all be in this file: https://github.com/MarkEdmondson1234/googleAnalyticsR/blob/master/R/ga_v4_objects.R , which essentially are just to shape the R list structure ready to be turned into JSON via jsonlite.

What can I do to help?

from ganalytics.

jdeboer avatar jdeboer commented on August 25, 2024

Thanks @MarkEdmondson1234 . Yes, from what it seems, v3 segments (which is what ganalytics is designed to support) can be translated into v4 segments. I think to a great extent v4 segments can also be translated back to v3, but this is limited to dimensions and operators that both versions share. For example, I don't believe v3 strictly has a 'ends with' operator and there is no explicit option for case sensitivity.

The idea is to be able to use the ganalytics syntax for writing filters and segments for use with googleAnalyticsR. This way ganalytics can benefit from many of the v4 API features too. I've started to put this into practice using the coercion methods above and these are working with I've tested so far.

Here's an example R script showing how the packages can be used together with the above coercion methods put into a source file I've called "ganalytics_to_googleAnalyticsR.R":

# Load the packages, coercion methods and authenticate with Google Analytics.
library(ganalytics)
creds <- GoogleApiCreds(appCreds = "client_secret.json")
options(googleAuthR.client_id = creds$app$key)
options(googleAuthR.client_secret = creds$app$secret)
library(googleAnalyticsR)
source("ganalytics_to_googleAnalyticsR.R")

# Identify the Google Analytics view to retrieve the data from.
view_id <- "117987738"

# Define the segments using ganalytics functions.
bounce_sessions <- Expr(~bounces != 0)
landed_on_home <- Expr(~landingPage == "/index.html")
bounced_on_home_page <- Include(bounce_sessions & landed_on_home)
didnt_bounce_on_home_page <- Exclude(bounced_on_home_page)
customers <- Include(Expr(~transactions > 0), scope = "users")

# Show how the segments would have looked in version 3 of the Google Analytics reporting API:
as(bounced_on_home_page, "character")
# [1] "sessions::condition::ga:bounces!=0;ga:landingPagePath==/index.html"
as(didnt_bounce_on_home_page, "character")
# [1] "sessions::condition::!ga:bounces!=0;ga:landingPagePath==/index.html"
as(customers, "character")
# [1] "users::condition::ga:transactions>0"

# Use the segments as version 4 segments.
google_analytics_4(
  view_id, c("2017-10-01", "2017-10-31"),
  metrics = c("users", "sessions", "bounces", "transactions", "transactionRevenue"),
  dimensions = c("date", "segment"),
  segments = list(
    segment_ga4("home_bounced",
                session_segment = as(bounced_on_home_page, "segmentDef_ga4")),
    segment_ga4("customers",
                user_segment = as(customers, "segmentDef_ga4")),
    segment_ga4("customers_home_bounced",
                user_segment = as(customers, "segmentDef_ga4"),
                session_segment = as(bounced_on_home_page, "segmentDef_ga4")),
    segment_ga4("customers_not_home_bounced",
                user_segment = as(customers, "segmentDef_ga4"),
                session_segment = as(didnt_bounce_on_home_page, "segmentDef_ga4"))
  )
)

I think we could go a lot further with this by implementing more class coercion methods and also by extending the ganalytics grammar and vocabulary to fully support v4 API features. I'm also wondering how the coercion methods should be implemented so that users don't need to worry about how to use them? To put them under the hood so to speak. Where should they belong, and how can they be supported by googleAnalyticsR?

from ganalytics.

jdeboer avatar jdeboer commented on August 25, 2024

I've updated the ganalytics classes to cover V4 features and will add the above methods to the package to coerce to googleAnalyticsR classes. I'll also add a function to conveniently use ganalytics objects like segments and filters with googleAnalyticsR, e.g. as_gar(my_segment). Or the other idea is to define a function that wraps google_analytics_4(...) to do the translations for the user behind the scenes.

from ganalytics.

MarkEdmondson1234 avatar MarkEdmondson1234 commented on August 25, 2024

Would it be useful to export any of more of the underlying functions of googleAnalyticsR ? Another way to approach segments is very welcome .

Does the grammar cover the sequence type segments as well?

from ganalytics.

jdeboer avatar jdeboer commented on August 25, 2024

Yes, I agree, if you could export the classes defined in your package that would then allow me to import them into the ganalytics package so I can define coercion methods there. I've created a pull request (#142) for googleAnalyticsR to export the classes - in that pull request I've exported them as S4 classes, as that is what I use in ganalytics.

In the ganalytics/dev branch I've imported the googleAnalyticsR classes and have added methods to coerce from ganalytics classes to googleAnalyticsR classes.

Here's an example of a sequential segment defined with ganalytics and used by googleAnalyticsR:

library(ganalytics)
creds <- GoogleApiCreds(appCreds = "~/client_secret.json")
options(googleAuthR.client_id = creds$app$key)
options(googleAuthR.client_secret = creds$app$secret)
library(googleAnalyticsR)

view_id <- "117987738"
checkout1 <- Expr(~pagePath == "/checkout_step_1")
checkout2 <- Expr(~pagePath == "/checkout_step_2")
checkout_progression <- Include(Sequence(checkout1, checkout2))
google_analytics_4(
  view_id, c("2017-10-01", "2017-10-31"),
  metrics = c("users", "sessions", "bounces", "transactions", "transactionRevenue"),
  dimensions = c("date", "segment"),
  segments = list(
    segment_ga4("checkout_progression",
                session_segment = as(checkout_progression, "segmentDef_ga4"))
  )
)

from ganalytics.

jdeboer avatar jdeboer commented on August 25, 2024

See https://github.com/MarkEdmondson1234/googleAnalyticsR/pull/176

from ganalytics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.