Learn business growth with Google Analytics 4 Forums Google Analytics 4 Implementing User Permissions Management with GA4 Admin API and Apps Script

  • Implementing User Permissions Management with GA4 Admin API and Apps Script

    Posted by Levi on 5 January 2023 at 5:06 am

    So, I’m working with the v1 alpha GA Admin API using the AnalyticsAdmin Service in AppScript, right? My project is all about managing permissions for a bunch of users right from Google Sheets – pretty cool, huh? I managed to list all user links on properties without a hitch, but I hit a snag when I tried to add a new user. I got an ‘Error 400’ message that says, “The value for the ‘direct_roles’ field was empty, but must be provided.” Bit of a head scratcher, right?

    I’ve got my own GCP project and I made sure the API is enabled for it, but I’m still running into this problem. Got any thoughts on why this could be happening? I’d appreciate any and all ideas!

    To give you an idea what I’m wrestling with, here’s a look at the request I’m building. I’m taking values right out of cells in Google Sheets to do it:

        request = [{
         parent: 'properties/' + entity[3],
         notifyNewUser: true,
         userLink: {
            emailAddress: entity[5],
            directRoles: entity[6]
    Ava replied 10 months, 4 weeks ago 3 Members · 2 Replies
  • 2 Replies
  • Emma

    11 February 2023 at 9:52 am

    It sounds like you’re trying to add a new user to your Google Analytics administration system directly from Google Sheets – which is a pretty creative solution! However, you’re getting a specific error message, “Error 400”. This error is generally associated with a “bad request,” meaning that your request to the server (in your case, to add a new user) cannot be understood or processed.

    The message’s additional information, “The value for the ‘direct_roles’ field was empty, but must be provided,” gives some idea of where things might be going wrong. This suggests that when you’re trying to add a new user, you need to assign them a “direct role” – this is presumably some kind of user designation in your system, and it cannot be left blank.

    Taking a look at your request, the “directRoles” value is being assigned from your Google Sheet. This error message might imply there’s a problem with that value. Maybe the relevant cell in your Google Sheet is empty, or perhaps there’s some kind of formatting issue that’s preventing the value from being read correctly. You might need to double-check your Google Sheet and make sure the “directRoles” values are presented correctly and consistently.

  • Ava

    13 June 2023 at 3:55 am

    The error message you received, “The value for the ‘direct_roles’ field was empty, but must be provided” suggests that the ‘direct_roles’ field in your request does not have a value when you are trying to add a new user. The ‘direct_roles’ field is expected to contain the roles that you want to assign to the user you’re adding.

    Considering the request body you’re using, it seems like you’re reading the roles from ‘entity[6]’ which appears to be an array. You need to ensure that ‘entity[6]’ contains the role or roles you want to assign to the user. It could be that ‘entity[6]’ is empty or undefined when you’re building your request, hence resulting in the error.

    In short, you need to make sure you’re providing valid roles in ‘entity[6]’ when building the request. You could add some checks or validations in your code to make sure ‘entity[6]’ is not empty or undefined before building the request, and that should resolve the 400 error.

Log in to reply.