如何在AnalyticsAdminserviceClient(GA4)Nodejs上修复Update_mask.Paths错误?

发布于 2025-02-08 09:19:47 字数 2674 浏览 3 评论 0原文

我尝试使用nodejs中的Google Analytics Admin Client(GA4)更新“ DatarEtentionSettings”,但是我收到以下错误。

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'
}

代码如下。

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();

我还将“名称”添加到UpdateMask的路径属性中,结果是相同的。

这是我提到的文档。 nofollow noreferrer

如何通过API更新DatarEtentionsettings?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2025-02-15 09:19:47

要更新GA 4中的属性,然后您可以尝试以下尝试:

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 snack 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);

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 snack 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);
泪冰清 2025-02-15 09:19:47

在C#中,

Google.Apis.Admin.AnalyticsData.v1alpha.Property property = service.Properties.Get($"properties/{propertyId}").Execute();

// Update the display name
property.DisplayName = newDisplayName;

// Build the update mask
var updateMask = new Google.Protobuf.WellKnownTypes.FieldMask();
updateMask.Paths.Add("display_name");

// Build the request
var request = service.Properties.Update(property, $"properties/{propertyId}");
request.UpdateMask = updateMask.Paths;

// Execute the request
request.Execute();

技巧是request.updatemask = updatemask.paths而不是request.updatemask = updateMask

In C#,

Google.Apis.Admin.AnalyticsData.v1alpha.Property property = service.Properties.Get(
quot;properties/{propertyId}").Execute();

// Update the display name
property.DisplayName = newDisplayName;

// Build the update mask
var updateMask = new Google.Protobuf.WellKnownTypes.FieldMask();
updateMask.Paths.Add("display_name");

// Build the request
var request = service.Properties.Update(property, 
quot;properties/{propertyId}");
request.UpdateMask = updateMask.Paths;

// Execute the request
request.Execute();

The trick is request.UpdateMask = updateMask.Paths instead of request.UpdateMask = updateMask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文