googleAnalyticsR icon indicating copy to clipboard operation
googleAnalyticsR copied to clipboard

Support for GA4 version of user deletion request (working patch inside)

Open zjuul opened this issue 1 year ago • 1 comments

Tried to do a bulk user deletion, but found out the API for GA4 works a tiny bit differently, and googleAnalyticsR does not work.

The below patch will make it work by adding an new allowed propertyType parameter to ga_clientid_deletion - and also handles the API response correctly.

It works on my local machine:

deletions <- ga_clientid_deletion(
  client_ids,
  ga4_propertyid,
  idType = "CLIENT_ID",
  propertyType = "ga4"
)

Got stuck updating the documentation, so this is a code-only patch. If you can include this functionality to the package, that would be awesome. Hope my code helps.

git diff output below:

diff --git a/R/client_id.R b/R/client_id.R
index 7a53375..e3c7144 100644
--- a/R/client_id.R
+++ b/R/client_id.R
@@ -73,7 +73,7 @@ ga_clientid_hash <- function(webPropertyId, clientId){
 ga_clientid_deletion <- function(userId,
                              propertyId,
                              idType = c("CLIENT_ID", "USER_ID", "APP_INSTANCE_ID"),
-                             propertyType= c("ga", "firebase")){
+                             propertyType= c("ga", "firebase", "ga4")){
   idType       <- match.arg(idType)
   propertyType <- match.arg(propertyType)
   
@@ -97,7 +97,7 @@ ga_clientid_deletion <- function(userId,
 ga_user_deletion_one <- function(userId,
                                  propertyId,
                                  idType = c("CLIENT_ID", "USER_ID", "APP_INSTANCE_ID"),
-                                 propertyType= c("ga", "firebase")){
+                                 propertyType= c("ga", "firebase", "ga4")){
   
   idType       <- match.arg(idType)
   propertyType <- match.arg(propertyType)
@@ -119,7 +119,9 @@ ga_user_deletion_one <- function(userId,
     body <- c(body, list(webPropertyId = propertyId))
   } else if(propertyType == "firebase"){
     body <- c(body, list(firebaseProjectId = propertyId))    
-  } 
+  } else if( propertyType == "ga4"){
+    body <- c(body, list(propertyId = propertyId))
+  }
   
   the_url <- "https://www.googleapis.com/analytics/v3/userDeletion/userDeletionRequests:upsert"
   
@@ -147,10 +149,14 @@ parse_user_deletion <- function(x){
   }
   
   if(!is.null(x$webPropertyId)){
+    # ga
     property <- x$webPropertyId
-  } else {
+  } else if(!is.null(x$firebaseProjectId)) {
     # firebase
     property <- x$firebaseProjectId
+  } else {
+    # ga4
+    property <- x$propertyId
   }
   
   data.frame(

zjuul avatar Oct 11 '23 20:10 zjuul