মৌলিক ব্যবহার

ক্লায়েন্ট লাইব্রেরির মৌলিক ব্যবহার নিম্নরূপ:


// Initialize a GoogleAdsConfig class.
GoogleAdsConfig config = new GoogleAdsConfig()
{
    DeveloperToken = "******",
    OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
    OAuth2SecretsJsonPath = "PATH_TO_CREDENTIALS_JSON",
    LoginCustomerId = ******
};

// Initialize a GoogleAdsClient class.
GoogleAdsClient client = new GoogleAdsClient(config);

// Create the required service.
CampaignServiceClient campaignService =
    client.GetService(Services.V23.CampaignService);

// Make more calls to service class.

একটি GoogleAdsClient ইনস্ট্যান্স তৈরি করুন

Google Ads API .NET লাইব্রেরির সবচেয়ে গুরুত্বপূর্ণ ক্লাস হল GoogleAdsClient ক্লাস। এটি আপনাকে একটি পূর্ব-কনফিগার করা পরিষেবা ক্লাস তৈরি করতে দেয় যা API কল করার জন্য ব্যবহার করা যেতে পারে। একটি GoogleAdsClient অবজেক্ট কনফিগার করতে, আপনাকে একটি GoogleAdsConfig অবজেক্ট তৈরি করতে হবে এবং প্রয়োজনীয় সেটিংস সেট করতে হবে। আরও জানতে কনফিগারেশন গাইডটি পড়ুন।


// Initialize a GoogleAdsConfig class.
GoogleAdsConfig config = new GoogleAdsConfig()
{
    DeveloperToken = "******",
    OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
    OAuth2SecretsJsonPath = "PATH_TO_CREDENTIALS_JSON",
    LoginCustomerId = ******
};

// Initialize a GoogleAdsClient class.
GoogleAdsClient client = new GoogleAdsClient(config);

// Modify the GoogleAdsClient afterwards.

client.Config.LoginCustomerId = ******;

একটি পরিষেবা তৈরি করুন

GoogleAdsClient একটি GetService পদ্ধতি প্রদান করে যা একটি বিজ্ঞাপন পরিষেবা তৈরি করতে ব্যবহার করা যেতে পারে।

CampaignServiceClient campaignService = client.GetService(
    Services.V23.CampaignService);
// Now make calls to CampaignService.

আমরা একটি Services ক্লাস প্রদান করি যা সমস্ত সমর্থিত API সংস্করণ এবং পরিষেবাগুলিকে তালিকাভুক্ত করে। GetService পদ্ধতি পরিষেবা তৈরি করার সময় এই গণনা বস্তুগুলিকে আর্গুমেন্ট হিসাবে গ্রহণ করে। উদাহরণস্বরূপ, Google Ads API এর V23 সংস্করণের জন্য CampaignServiceClient এর একটি উদাহরণ তৈরি করতে, আপনাকে পূর্ববর্তী উদাহরণে দেখানো হিসাবে Services.V23.CampaignService সহ GoogleAdsClient.GetService পদ্ধতিটিকে আর্গুমেন্ট হিসাবে কল করতে হবে।

ত্রুটি পরিচালনা

প্রতিটি API কল সফল হবে না। যদি আপনার API কলগুলি কোনও কারণে ব্যর্থ হয় তবে সার্ভার ত্রুটিগুলি ফেলতে পারে। API ত্রুটিগুলি ক্যাপচার করা এবং সেগুলি যথাযথভাবে পরিচালনা করা গুরুত্বপূর্ণ।

যখন কোনও API ত্রুটি ঘটে তখন একটি GoogleAdsException ইনস্ট্যান্স থ্রো করা হয়। কী ভুল হয়েছে তা বের করতে আপনাকে সাহায্য করার জন্য এতে বিশদ বিবরণ রয়েছে:

public void Run(GoogleAdsClient client, long customerId)
{
    // Get the GoogleAdsService.
    GoogleAdsServiceClient googleAdsService = client.GetService(
        Services.V23.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 client1 = new GoogleAdsClient();
GoogleAdsClient client2 = new GoogleAdsClient();

Thread userThread1 = new Thread(addAdGroups);
Thread userThread2 = new Thread(addAdGroups);

userThread1.start(client1);
userThread2.start(client2);

userThread1.join();
userThread2.join();

public void addAdGroups(object data) {
  GoogleAdsClient client = (GoogleAdsClient) data;
  // Do more operations here.
  ...
}

আপনার অ্যাপ্লিকেশনটি প্রতিক্রিয়াশীল রাখুন

অনুরোধগুলি কত বড় তার উপর নির্ভর করে Google Ads API API পদ্ধতি কলগুলি সম্পূর্ণ হতে কিছুটা সময় লাগতে পারে। আপনার অ্যাপ্লিকেশনটি প্রতিক্রিয়াশীল রাখতে, আমরা নিম্নলিখিত পদক্ষেপগুলি অনুসরণ করার পরামর্শ দিচ্ছি:

Grpc.Core লাইব্রেরি ব্যবহার করুন

যদি আপনি এমন একটি অ্যাপ্লিকেশন তৈরি করেন যা .NET ফ্রেমওয়ার্ককে লক্ষ্য করে এবং ASP.NET ওয়েব ফর্ম বা WinForms অ্যাপ্লিকেশনের মতো একটি লিগ্যাসি প্রযুক্তি ব্যবহার করে, তাহলে আমরা লিগ্যাসি Grpc.Core লাইব্রেরিটি নিম্নরূপ ব্যবহার করার পরামর্শ দিচ্ছি:

   GoogleAdsConfig config = new GoogleAdsConfig();
   config.UseGrpcCore = true;
   GoogleAdsClient client = new GoogleAdsClient(config);
   ```

### Use the asynchronous methods {: #asynchronous}

You can use asynchronous methods to keep your application responsive. Here are a
couple of examples.

#### Retrieve the list of campaigns, and populate a `ListView`

```c#
private async void button1_Click(object sender, EventArgs e)
{
    // Get the GoogleAdsService.
    GoogleAdsServiceClient googleAdsService = client.GetService(
        Services.V23.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>();
    Task t =  googleAdsService.SearchStreamAsync(customerId.ToString(), query,
        delegate (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);
            }
        }
    );
    await t;
    listView1.Items.AddRange(items.ToArray());
}
```

#### Update a campaign budget and display a Message box alert

```c#
private async void button2_Click(object sender, EventArgs e)
{
    // Get the BudgetService.
    CampaignBudgetServiceClient budgetService = client.GetService(
        Services.V23.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.
    Task<MutateCampaignBudgetsResponse> t = budgetService.MutateCampaignBudgetsAsync(
        customerId.ToString(), new CampaignBudgetOperation[] { budgetOperation });

    await t;
    MutateCampaignBudgetsResponse response = t.Result;
    MessageBox.Show(response.Results[0].ResourceName);
}
```