How can I fix update_mask.paths error on AnalyticsAdminServiceClient(GA4) Nodejs?

Viewed 99

I tried to update "DataRetentionSettings" using Google Analytics Admin Client(GA4) in Nodejs, but I got the following error.

Error: 3 INVALID_ARGUMENT: One or more values in the field 'update_mask.paths_list' was invalid, but all values must be valid.eventDataRetention, resetUserDataOnNewActivity
    at Object.callErrorFromStatus (C:\my_working_path\GA4_manager\node_modules\@grpc\grpc-js\build\src\call.js:31:26)
    at Object.onReceiveStatus (C:\my_working_path\GA4_manager\node_modules\@grpc\grpc-js\build\src\client.js:189:52)
    at Object.onReceiveStatus (C:\my_working_path\GA4_manager\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:365:141)
    at Object.onReceiveStatus (C:\my_working_path\GA4_manager\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:328:181)
    at C:\my_working_path\GA4_manager\node_modules\@grpc\grpc-js\build\src\call-stream.js:187:78
    at processTicksAndRejections (node:internal/process/task_queues:78:11) {
  code: 3,
  details: "One or more values in the field 'update_mask.paths_list' was invalid, but all values must be valid.eventDataRetention, resetUserDataOnNewActivity",
  metadata: Metadata {
    internalRepr: Map(1) { 'grpc-server-stats-bin' => [Array] },
    options: {}
  },
  note: 'Exception occurred in retry method that was not classified as transient'
}

The code is as follows.

const analyticsAdmin = require("@google-analytics/admin");

class Main {
    constructor() {
        this.analyticsAdminClient = new analyticsAdmin.AnalyticsAdminServiceClient({
            keyFilename: "./mykeyfile.json",
        });
    }
    async updateDataRetentionSettings() {
        const name = "properties/*********/dataRetentionSettings";
        const request = {
            dataRetentionSettings: {
                name: name,
                eventDataRetention: "FOURTEEN_MONTHS",
                resetUserDataOnNewActivity: true,
            },
            updateMask: {
                paths: ["eventDataRetention", "resetUserDataOnNewActivity"],
            },
        };
        let retention = {};
        try {
            retention = await this.analyticsAdminClient.updateDataRetentionSettings(request);
        } catch (e) {
            console.log(e);
            process.exit(1);
        }
        return retention[0];
    }
}

const client = new Main();
client.updateDataRetentionSettings();

I also added "name" to the paths property of updateMask and the result was the same.

Here is the document I referred to. AnalyticsAdminServiceClient

And the client version is 4.0.0.

How can I update DataRetentionSettings via API?

1 Answers

To update property in GA 4 then you could try as follows :

const {AnalyticsAdminServiceClient} = require('@google-analytics/admin').v1alpha; // ---> This dependency should be installed
const credentialFile = '/usr/local/credentialFile.json';
 const adminClient = new AnalyticsAdminServiceClient(
        {keyFilename: credentialFile} // --> credentialFile will be the path of service account's detail json file in your local machine
    );

    async function callUpdateProperty() {
        // Construct request
        const updateMask = {
            paths: ["display_name"] // --> Please keep in mind that name should in camel case. like I have added for 'displayName' as 'display_name'
        };
        const property = {
            "name" : "properties/123",
            "displayName": "New Display Name"
        };

        const request = {
            property,
            updateMask,
        };

        // Run request
        const response = await adminClient.updateProperty(request);
Related