.NET क्लाइंट लाइब्रेरी का बुनियादी इस्तेमाल इस तरह किया जाता है:
// Initialize a GoogleAdsConfig instance.
GoogleAdsConfig config = new GoogleAdsConfig()
{
OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
OAuth2SecretsJsonPath = "PATH_TO_CREDENTIALS_JSON",
LoginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"
};
// Initialize a GoogleAdsClient instance.
GoogleAdsClient client = new GoogleAdsClient(config);
// Create the required service.
CampaignServiceClient campaignService =
client.GetService(Services.V25.CampaignService);
// Make calls to the service client.
क्लाइंट और सेवाओं को शुरू करना
Google Ads API के साथ इंटरैक्ट करने के लिए, सबसे पहले GoogleAdsClient को कॉन्फ़िगर करें और इंस्टैंटिएट करें. इसके बाद, इसका इस्तेमाल करके उन एपीआई सेवा क्लाइंट को बनाएं जिनकी आपको ज़रूरत है.
GoogleAdsClient इंस्टेंस बनाना
Google Ads API की .NET लाइब्रेरी में सबसे अहम क्लास, GoogleAdsClient
क्लास होती है. इसकी मदद से, पहले से कॉन्फ़िगर किया गया एक ऐसा सर्विस क्लाइंट बनाया जा सकता है जिसका इस्तेमाल एपीआई कॉल करने के लिए किया जा सकता है. GoogleAdsClient ऑब्जेक्ट को कॉन्फ़िगर करने के लिए, GoogleAdsConfig ऑब्जेक्ट बनाएं और ज़रूरी प्रॉपर्टी सेट करें. ज़्यादा जानने के लिए, कॉन्फ़िगरेशन गाइड देखें.
// Initialize a GoogleAdsConfig instance.
GoogleAdsConfig config = new GoogleAdsConfig()
{
OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
OAuth2SecretsJsonPath = "PATH_TO_CREDENTIALS_JSON",
LoginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"
};
// Initialize a GoogleAdsClient instance.
GoogleAdsClient client = new GoogleAdsClient(config);
// Modify the GoogleAdsClient configuration afterwards if needed.
client.Config.LoginCustomerId = "INSERT_UPDATED_LOGIN_CUSTOMER_ID_HERE";
कोई सेवा बनाना
GoogleAdsClient एक GetService तरीका उपलब्ध कराता है. इसका इस्तेमाल, एपीआई सेवा क्लाइंट बनाने के लिए किया जा सकता है.
CampaignServiceClient campaignService = client.GetService(
Services.V25.CampaignService);
// Now make calls to CampaignService.
यह लाइब्रेरी, Services क्लास उपलब्ध कराती है. यह क्लास, एपीआई के सभी वर्शन और सेवाओं के बारे में जानकारी देती है. इसमें v25.1 जैसे माइनर रिलीज़, अपने मेजर वर्शन एनम Services.V25 का इस्तेमाल करते हैं. GetService वाला तरीका, सेवा बनाते समय इन इन्यूमरेशन ऑब्जेक्ट को आर्ग्युमेंट के तौर पर स्वीकार करता है. उदाहरण के लिए, Google Ads API के V25 वर्शन के लिए CampaignServiceClient का इंस्टेंस बनाने के लिए, GoogleAdsClient.GetService तरीके को Services.V25.CampaignService के साथ आर्ग्युमेंट के तौर पर कॉल करें. जैसा कि ऊपर दिए गए उदाहरण में दिखाया गया है.
गड़बड़ी ठीक करना
ऐसा ज़रूरी नहीं है कि हर एपीआई कॉल पूरा हो. अगर किसी वजह से आपके एपीआई कॉल पूरे नहीं हो पाते, तो सर्वर गड़बड़ी के मैसेज दिखा सकता है. एपीआई की गड़बड़ियों को कैप्चर करना और उन्हें सही तरीके से मैनेज करना ज़रूरी है.
एपीआई से जुड़ी गड़बड़ी होने पर, GoogleAdsException इंस्टेंस थ्रो किया जाता है. इसमें यह जानकारी होती है, ताकि आपको यह पता चल सके कि क्या गड़बड़ी हुई है:
public void Run(GoogleAdsClient client, long customerId) { // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsService = client.GetService( Services.V25.GoogleAdsService); // Create a query that will retrieve all campaigns. string query = @"SELECT campaign.id, campaign.name, campaign.network_settings.target_content_network FROM campaign ORDER BY campaign.id"; try { // Issue a search request. googleAdsService.SearchStream(customerId.ToString(), query, delegate (SearchGoogleAdsStreamResponse resp) { foreach (GoogleAdsRow googleAdsRow in resp.Results) { Console.WriteLine("Campaign with ID {0} and name '{1}' was found.", googleAdsRow.Campaign.Id, googleAdsRow.Campaign.Name); } } ); } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } }
थ्रेड सुरक्षा
शेयर किए गए GoogleAdsClient इंस्टेंस की कॉन्फ़िगरेशन स्थिति को कई थ्रेड में बदलने से, थ्रेड सुरक्षित नहीं रहती. ऐसा इसलिए, क्योंकि एक थ्रेड में किसी इंस्टेंस के कॉन्फ़िगरेशन में किए गए बदलावों का असर, दूसरी थ्रेड में बनाई गई सेवाओं पर पड़ सकता है.
हालांकि, रीड-ओनली ऑपरेशन थ्रेड-सेफ़ होते हैं. जैसे, GoogleAdsClient के ऐसे इंस्टेंस से नए सेवा इंस्टेंस पाना जिनमें कोई बदलाव नहीं किया गया है और एक साथ कई सेवाओं को कॉल करना.
हर थ्रेड के कॉन्फ़िगरेशन में होने वाले बदलावों को अलग करने के लिए, हर वर्कर टास्क या थ्रेड के लिए एक अलग GoogleAdsClient इंस्टैंटिएट करें:
GoogleAdsClient client1 = new GoogleAdsClient();
GoogleAdsClient client2 = new GoogleAdsClient();
Task task1 = Task.Run(() => AddAdGroups(client1));
Task task2 = Task.Run(() => AddAdGroups(client2));
await Task.WhenAll(task1, task2);
public void AddAdGroups(GoogleAdsClient client)
{
// Perform operations with client.
}
अपने ऐप्लिकेशन को रिस्पॉन्सिव बनाए रखना
Google Ads API के तरीके को कॉल करने में कुछ समय लग सकता है. यह इस बात पर निर्भर करता है कि अनुरोध कितने बड़े हैं. अपने ऐप्लिकेशन को रिस्पॉन्सिव बनाए रखने के लिए, यह तरीका अपनाएं:
लेगसी यूज़र इंटरफ़ेस (यूआई) फ़्रेमवर्क के लिए, Grpc.Core लाइब्रेरी का इस्तेमाल करना
अगर आपको .NET Framework को टारगेट करने वाला कोई ऐसा ऐप्लिकेशन डेवलप करना है जो ASP.NET Web Forms या WinForms जैसी लेगसी यूज़र इंटरफ़ेस टेक्नोलॉजी का इस्तेमाल करता है, तो लेगसी Grpc.Core ट्रांसपोर्ट लाइब्रेरी को इस तरह से चालू किया जा सकता है:
GoogleAdsConfig config = new GoogleAdsConfig();
config.UseGrpcCore = true;
GoogleAdsClient client = new GoogleAdsClient(config);
एसिंक्रोनस तरीकों का इस्तेमाल करना
अपने ऐप्लिकेशन को रिस्पॉन्सिव बनाए रखने के लिए, एसिंक्रोनस तरीकों का इस्तेमाल किया जा सकता है. यहां कुछ उदाहरण दिए गए हैं.
कैंपेन की सूची वापस पाना और ListView को पॉप्युलेट करना
private async void OnRetrieveCampaignsButtonClick(object sender, EventArgs e)
{
try
{
// Get the GoogleAdsService.
GoogleAdsServiceClient googleAdsService = client.GetService(
Services.V25.GoogleAdsService);
// Create a query that will retrieve all campaigns.
string query = @"SELECT
campaign.id,
campaign.name,
campaign.network_settings.target_content_network
FROM campaign
ORDER BY campaign.id";
List<ListViewItem> items = new List<ListViewItem>();
await googleAdsService.SearchStreamAsync(
customerId.ToString(),
query,
(SearchGoogleAdsStreamResponse resp) =>
{
foreach (GoogleAdsRow googleAdsRow in resp.Results)
{
ListViewItem item = new ListViewItem();
item.Text = googleAdsRow.Campaign.Id.ToString();
item.SubItems.Add(googleAdsRow.Campaign.Name);
items.Add(item);
}
}
);
listView1.Items.AddRange(items.ToArray());
}
catch (GoogleAdsException ex)
{
MessageBox.Show($"API Error: {ex.Message}");
}
}
कैंपेन का बजट अपडेट करना और मैसेज बॉक्स में सूचना दिखाना
private async void OnUpdateBudgetButtonClick(object sender, EventArgs e)
{
try
{
// Get the CampaignBudgetService.
CampaignBudgetServiceClient budgetService = client.GetService(
Services.V25.CampaignBudgetService);
// Create the campaign budget.
CampaignBudget budget = new CampaignBudget()
{
Name = "Interplanetary Cruise Budget #" +
ExampleUtilities.GetRandomString(),
DeliveryMethod = BudgetDeliveryMethod.Standard,
AmountMicros = 500000
};
// Create the operation.
CampaignBudgetOperation budgetOperation = new CampaignBudgetOperation()
{
Create = budget
};
// Create the campaign budget asynchronously.
MutateCampaignBudgetsResponse response =
await budgetService.MutateCampaignBudgetsAsync(
customerId.ToString(),
new CampaignBudgetOperation[] { budgetOperation });
MessageBox.Show(response.Results[0].ResourceName);
}
catch (GoogleAdsException ex)
{
MessageBox.Show($"API Error: {ex.Message}");
}
}