با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
در Google Ads API، به روز رسانی ها با استفاده از یک فیلد ماسک انجام می شود. فیلد ماسک تمام فیلدهایی را که میخواهید با بهروزرسانی تغییر دهید فهرست میکند و هر فیلد مشخص شده که در فیلد ماسک نیست نادیده گرفته میشود، حتی اگر به سرور ارسال شود.
کمک کننده ماسک میدانی
روش پیشنهادی برای تولید ماسکهای فیلد استفاده از تابع helper field_mask موجود در بسته google.api_core است. دو شیء protobuf را می پذیرد و یک شیء ماسک فیلد را با یک فیلد list برمی گرداند که شامل تمام فیلدهایی است که بین دو شیء متفاوت است.
اگر None بهعنوان پارامتر اول ارسال شود، فهرست فیلد ماسک فقط شامل تمام فیلدهای روی دومین شی protobuf است که روی مقدار پیشفرض تنظیم نشدهاند.
پس از ساخته شدن، شیء فیلد ماسک باید بر روی شی عملیاتی که به سرور ارسال می شود کپی شود:
در اینجا یک مثال برای به روز رسانی یک کمپین آورده شده است:
fromgoogle.api_coreimportprotobuf_helpersfromgoogle.ads.googleads.clientimportGoogleAdsClient# Retrieve a GoogleAdsClient instance.client=GoogleAdsClient.load_from_storage()# Create a new campaign operation.campaign_operation=client.get_type('CampaignOperation')# Retrieve a new campaign object from its update field.campaign=campaign_operation.update# Mutate the campaign.campaign.network_settings.target_search_network.value=False# Create a field mask using the updated campaign.# The field_mask helper is only compatible with raw protobuf message# instances, which we can access using the ._pb attribute.field_mask=protobuf_helpers.field_mask(None,campaign._pb)# Copy the field_mask onto the operation's update_mask field.client.copy_from(campaign_operation.update_mask,field_mask)
ابتدا یک شی CampaignOperation خالی ایجاد می کنیم. سپس، یک شیء خالی Campaign را از آن بازیابی می کنیم. سپس آن شی کمپین را بهروزرسانی میکنیم و یک فیلد ماسک جدید ایجاد میکنیم و آن را با None مقایسه میکنیم، که یک لیست ماسک فیلد ایجاد میکند که فقط حاوی فیلد network_settings.target_search_network است که تغییر کرده است.
در اینجا نمونه ای از به روز رسانی یک کمپین موجود است. در اینجا فرض می کنیم که اسکریپت یک پارامتر resource_name ارائه شده است که یک نام منبع معتبر برای یک کمپین و یک customer_id معتبر است:
importprotofromgoogle.api_coreimportprotobuf_helpersfromgoogle.ads.googleads.clientimportGoogleAdsClient# Retrieve a GoogleAdsClient instance.client=GoogleAdsClient.load_from_storage()# Retrieve an instance of the GoogleAdsService.googleads_service=client.get_service('GoogleAdsService')# Search query to retrieve campaign.query=f""" SELECT campaign.network_settings.target_search_network, campaign.resource_name FROM campaign WHERE campaign.resource_name = {resource_name}"""# Submit a query to retrieve a campaign instance.response=googleads_service.search_stream(customer_id=customer_id,query=query)# Iterate over results to retrieve the campaign.forbatchinresponse:forrowinbatch.results:initial_campaign=row.campaign# Create a new campaign operation.campaign_operation=client.get_type('CampaignOperation')# Set the copied campaign object to a variable for easy reference.updated_campaign=campaign_operation.update# Copy the retrieved campaign into the new campaign.# Here we use the proto.Message.copy_from method because of its simple# compatibility with the protobuf message instances, which are wrapped# by the proto-plus library.proto.Message.copy_from(updated_campaign,initial_campaign)# Mutate the new campaign.updated_campaign.network_settings.target_search_network=False# Create a field mask using the updated campaign.field_mask=protobuf_helpers.field_mask(initial_campaign._pb,updated_campaign._pb)# Copy the field mask onto the operation's update_mask field.# Note that the client's copy_from method is designed to work with both native# messages and messages wrapped by proto-plus, so it works here for the# update_mask, even though it's an instance of the native message class# google.protobuf.field_mask_pb2.FieldMask.client.copy_from(campaign_operation.update_mask,field_mask)
با استفاده از این استراتژی، updated_campaign همه initial_campaign مشابهی که از API بازیابی شده است، یعنی نام منبع را به اشتراک خواهد گذاشت. فیلد ماسک ایجاد شده به API می گوید که فقط فیلد network_settings.target_search_network باید تغییر کند.
تاریخ آخرین بهروزرسانی 2025-09-03 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-03 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eUpdates in the Google Ads API utilize a field mask to specify the fields to be changed, ignoring any other fields sent to the server.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003efield_mask\u003c/code\u003e helper function from \u003ccode\u003egoogle.api_core\u003c/code\u003e simplifies field mask creation by comparing two protobuf objects.\u003c/p\u003e\n"],["\u003cp\u003eWhen \u003ccode\u003eNone\u003c/code\u003e is used as the first parameter in the \u003ccode\u003efield_mask\u003c/code\u003e helper, the generated mask includes only fields with non-default values.\u003c/p\u003e\n"],["\u003cp\u003eTo update an existing campaign, retrieve it first, modify the desired fields, and then utilize the \u003ccode\u003efield_mask\u003c/code\u003e helper for targeted updates.\u003c/p\u003e\n"],["\u003cp\u003eApply the generated field mask to the operation object's \u003ccode\u003eupdate_mask\u003c/code\u003e field before sending it to the server for processing.\u003c/p\u003e\n"]]],[],null,["# Updates Using Field Masks\n\nIn the Google Ads API, updates are done using a field mask. The field mask lists\nall the fields you intend to change with the update, and any specified fields\nthat are not in the field mask will be ignored, even if sent to the server.\n\nField mask helper\n-----------------\n\nThe recommended way to generate field masks is using the `field_mask` helper\nfunction included in the `google.api_core` package. It accepts two protobuf\nobjects and returns a field mask object with a `list` field that contains all\nof the fields that are different between the two objects.\n\nIf `None` is passed as the first parameter then the field mask list will just\ncontain all of the fields on the second protobuf object that are not set to\ntheir default value.\n\nOnce constructed the field mask object should be copied onto the operation\nobject that will be send to the server:\n\nHere's an example for updating a campaign: \n\n from google.api_core import protobuf_helpers\n from google.ads.googleads.client import GoogleAdsClient\n\n # Retrieve a GoogleAdsClient instance.\n client = GoogleAdsClient.load_from_storage()\n # Create a new campaign operation.\n campaign_operation = client.get_type('CampaignOperation')\n # Retrieve a new campaign object from its update field.\n campaign = campaign_operation.update\n # Mutate the campaign.\n campaign.network_settings.target_search_network.value = False\n\n # Create a field mask using the updated campaign.\n # The field_mask helper is only compatible with raw protobuf message\n # instances, which we can access using the ._pb attribute.\n field_mask = protobuf_helpers.field_mask(None, campaign._pb)\n\n # Copy the field_mask onto the operation's update_mask field.\n client.copy_from(campaign_operation.update_mask, field_mask)\n\nFirst, we create an empty CampaignOperation object. Then, we set retrieve an\nempty Campaign object from it. We then update that campaign object and create\na new field mask, comparing it to `None`, which will generate a field mask list\nthat only contains the `network_settings.target_search_network` field that was\nchanged.\n\nHere's an example updating an existing campaign. Here we assume the script has\nbeen provided a `resource_name` parameter that is a valid resource name for a\ncampaign and a valid `customer_id`: \n\n import proto\n\n from google.api_core import protobuf_helpers\n from google.ads.googleads.client import GoogleAdsClient\n\n # Retrieve a GoogleAdsClient instance.\n client = GoogleAdsClient.load_from_storage()\n # Retrieve an instance of the GoogleAdsService.\n googleads_service = client.get_service('GoogleAdsService')\n\n # Search query to retrieve campaign.\n query = f\"\"\"\n SELECT\n campaign.network_settings.target_search_network,\n campaign.resource_name\n FROM campaign\n WHERE campaign.resource_name = {resource_name}\"\"\"\n\n # Submit a query to retrieve a campaign instance.\n response = googleads_service.search_stream(customer_id=customer_id, query=query)\n\n # Iterate over results to retrieve the campaign.\n for batch in response:\n for row in batch.results:\n initial_campaign = row.campaign\n\n # Create a new campaign operation.\n campaign_operation = client.get_type('CampaignOperation')\n # Set the copied campaign object to a variable for easy reference.\n updated_campaign = campaign_operation.update\n # Copy the retrieved campaign into the new campaign.\n # Here we use the proto.Message.copy_from method because of its simple\n # compatibility with the protobuf message instances, which are wrapped\n # by the proto-plus library.\n proto.Message.copy_from(updated_campaign, initial_campaign)\n # Mutate the new campaign.\n updated_campaign.network_settings.target_search_network = False\n\n # Create a field mask using the updated campaign.\n field_mask = protobuf_helpers.field_mask(\n initial_campaign._pb, updated_campaign._pb\n )\n\n # Copy the field mask onto the operation's update_mask field.\n # Note that the client's copy_from method is designed to work with both native\n # messages and messages wrapped by proto-plus, so it works here for the\n # update_mask, even though it's an instance of the native message class\n # google.protobuf.field_mask_pb2.FieldMask.\n client.copy_from(campaign_operation.update_mask, field_mask)\n\nWith this strategy, the `updated_campaign` will share all the same fields as the\n`initial_campaign` that was retrieved from the API, namely the resource name.\nThe generated field mask will tell the API that only the\n`network_settings.target_search_network` field needs to be changed."]]