ไลบรารีไคลเอ็นต์

ไลบรารีไคลเอ็นต์ของเรานำเสนอมุมมองระดับสูงและองค์ประกอบพื้นฐานพื้นฐานของ ฟังก์ชันการทำงานของ Google Ads API ที่ช่วยให้พัฒนาแอปได้อย่างรวดเร็วและง่ายขึ้น คำแนะนำจากเรา เริ่มต้นด้วย API หากคุณเพิ่งเริ่มใช้ API

ไลบรารีของไคลเอ็นต์ แหล่งที่มา การจัดจำหน่าย ตัวอย่างโค้ด
Java google-ads-java Maven, tar.gz ดูใน GitHub
.NET google-ads-dotnet nuget, tar.gz, zip ดูใน GitHub
PHP google-ads-php tar.gz ดูใน GitHub
Python google-ads-python tar.gz, ZIP ดูใน GitHub
Ruby google-ads-ruby gem, tar.gz, zip ดูใน GitHub
Perl google-ads-perl tar.gz, ZIP ดูใน GitHub

เวอร์ชัน API ที่รองรับ

ตารางนี้แสดงไลบรารีของไคลเอ็นต์ที่ใช้ได้กับ API เวอร์ชันต่างๆ

Java

ไลบรารีของไคลเอ็นต์สำหรับ Java
v17 Min: 32.0.0
Max: -
v16 Min: 30.0.0
Max: -
v15 Min: 28.0.0
Max: -

C#

ไลบรารีของไคลเอ็นต์สำหรับ .NET
v17 Min: 20.1.0
Max: -
v16 Min: 18.1.0
Max: -
v15 Min: 17.1.0
Max: -

PHP

ไลบรารีไคลเอ็นต์สำหรับ PHP
v17 Min: 23.1.0
Max: -
v16 Min: 22.1.0
Max: -
v15 Min: 21.1.0
Max: -

Python

ไลบรารีของไคลเอ็นต์สำหรับ Python
v17 Min: 24.1.0
Max: -
v16 Min: 23.1.0
Max: -
v15 Min: 22.1.0
Max: -

Ruby

ไลบรารีไคลเอ็นต์สำหรับ Ruby
v17 Min: 29.0.0
Max: -
v16 Min: 27.0.0
Max: -
v15 Min: 25.0.0
Max: -

Perl

ไลบรารีของไคลเอ็นต์สำหรับ Perl
v17 Min: 23.0.0
Max: -
v16 Min: 21.0.0
Max: -
v15 Min: 19.0.0
Max: -

การกำหนดค่า

ไลบรารีของไคลเอ็นต์ Ads API แต่ละรายการมีการตั้งค่ากำหนดและ วิธีการโหลดที่คุณสามารถใช้ปรับแต่งลักษณะการทำงานของ URL ได้

ต่อไปนี้คือตัวแปรสภาพแวดล้อมทั่วไปในไลบรารีของไคลเอ็นต์ทั้งหมด ที่สามารถโหลดเพื่อตั้งค่าการกำหนดค่าได้

  • ไลบรารีของไคลเอ็นต์
    • GOOGLE_ADS_CONFIGURATION_FILE_PATH: เส้นทางไปยังไฟล์การกำหนดค่า
  • OAuth2
    • โหมดแอปพลิเคชัน
      • GOOGLE_ADS_CLIENT_ID : ตั้งค่านี้เป็นรหัสไคลเอ็นต์ OAuth2
      • GOOGLE_ADS_CLIENT_SECRET : ตั้งค่านี้เป็นรหัสลับไคลเอ็นต์ OAuth2 ของคุณ
      • GOOGLE_ADS_REFRESH_TOKEN : ตั้งค่านี้เป็นโทเค็นการรีเฟรช OAuth2 ที่สร้างไว้ล่วงหน้า ถ้าต้องการนำโทเค็น OAuth2 มาใช้ซ้ำ การตั้งค่านี้เป็นตัวเลือกที่ไม่บังคับ
    • โหมดบัญชีบริการ
      • GOOGLE_ADS_JSON_KEY_FILE_PATH : ตั้งค่านี้เป็นการกำหนดค่า JSON แบบ OAuth2 เส้นทางไฟล์
      • GOOGLE_ADS_IMPERSONATED_EMAIL : ตั้งค่านี้เป็นอีเมลของแอตทริบิวต์ บัญชีที่คุณแอบอ้าง
  • Google Ads API
    • GOOGLE_ADS_DEVELOPER_TOKEN : ตั้งค่านี้เป็นโทเค็นของนักพัฒนาของคุณ
    • GOOGLE_ADS_LOGIN_CUSTOMER_ID : นี่คือรหัสลูกค้าของลูกค้าที่ได้รับสิทธิ์ให้ใช้ ในคำขอโดยไม่มีเครื่องหมายขีดกลาง (-)
    • GOOGLE_ADS_LINKED_CUSTOMER_ID : ส่วนหัวนี้จำเป็นสำหรับเมธอดที่อัปเดตค่า ของเอนทิตีเมื่อได้รับสิทธิ์ผ่านบัญชีที่ลิงก์ใน UI ของ Google Ads (แหล่งข้อมูล AccountLink ใน Google Ads API) ตั้งค่านี้ กับรหัสลูกค้าของผู้ให้บริการข้อมูลที่อัปเดตทรัพยากรของ รหัสลูกค้าที่ระบุ ซึ่งควรตั้งค่าโดยไม่มีขีดกลาง (-) เพื่อดูข้อมูลเพิ่มเติม เกี่ยวกับบัญชีที่ลิงก์ โปรดไปที่ศูนย์ช่วยเหลือ

โดยทั่วไปแล้วจะมีการกำหนดตัวแปรสภาพแวดล้อมไว้ในไฟล์การกำหนดค่า Bash เช่น เป็นไฟล์ .bashrc หรือ .bash_profile ในไดเรกทอรี $HOME โฆษณาเหล่านี้ สามารถกำหนดได้โดยใช้บรรทัดคำสั่ง

ขั้นตอนพื้นฐานในการกำหนดตัวแปรสภาพแวดล้อมโดยใช้ .bashrc มีดังนี้ โดยใช้เทอร์มินัล:

# Append the line "export GOOGLE_ADS_CLIENT_ID=1234567890" to
# the bottom of your .bashrc file.
echo "export GOOGLE_ADS_CLIENT_ID=1234567890" >> ~/.bashrc

# Update your bash environment to use the most recently updated
# version of your .bashrc file.
src ~/.bashrc

นอกจากนี้ ยังตั้งค่าตัวแปรสภาพแวดล้อมในอินสแตนซ์เทอร์มินัลได้โดยตรงจาก บรรทัดคำสั่งดังนี้

export GOOGLE_ADS_CLIENT_ID=1234567890

อีกทางเลือกหนึ่งคือการตั้งค่าตัวแปรสภาพแวดล้อมเมื่อเรียกใช้คำสั่ง ที่ใช้ผลิตภัณฑ์และบริการต่อไปนี้

GOOGLE_ADS_CLIENT_ID=1234567890 php /path/to/script/that/uses/envvar.php

ค้นหาการใส่เลขหน้า

ราคาปกติของ GoogleAdsService.Search ใช้ในแอปแบบอินเทอร์แอกทีฟที่แสดงหน้าผลการค้นหา

ไลบรารีไคลเอ็นต์ของเรา จะปรับใช้ การแบ่งหน้า เมื่อคุณปรับปรุงผลลัพธ์เพื่อให้คุณสามารถ ดาวน์โหลดและประมวลผลทั้งหมดพร้อมกันตามลำดับ เช่น

Java

private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
  try (GoogleAdsServiceClient googleAdsServiceClient =
      googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
    String query = "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id";
    // Constructs the SearchGoogleAdsStreamRequest.
    SearchGoogleAdsStreamRequest request =
        SearchGoogleAdsStreamRequest.newBuilder()
            .setCustomerId(Long.toString(customerId))
            .setQuery(query)
            .build();

    // Creates and issues a search Google Ads stream request that will retrieve all campaigns.
    ServerStream<SearchGoogleAdsStreamResponse> stream =
        googleAdsServiceClient.searchStreamCallable().call(request);

    // Iterates through and prints all of the results in the stream response.
    for (SearchGoogleAdsStreamResponse response : stream) {
      for (GoogleAdsRow googleAdsRow : response.getResultsList()) {
        System.out.printf(
            "Campaign with ID %d and name '%s' was found.%n",
            googleAdsRow.getCampaign().getId(), googleAdsRow.getCampaign().getName());
      }
    }
  }
}
      

C#

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

PHP

public static function runExample(GoogleAdsClient $googleAdsClient, int $customerId)
{
    $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
    // Creates a query that retrieves all campaigns.
    $query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';
    // Issues a search stream request.
    /** @var GoogleAdsServerStreamDecorator $stream */
    $stream = $googleAdsServiceClient->searchStream(
        SearchGoogleAdsStreamRequest::build($customerId, $query)
    );

    // Iterates over all rows in all messages and prints the requested field values for
    // the campaign in each row.
    foreach ($stream->iterateAllElements() as $googleAdsRow) {
        /** @var GoogleAdsRow $googleAdsRow */
        printf(
            "Campaign with ID %d and name '%s' was found.%s",
            $googleAdsRow->getCampaign()->getId(),
            $googleAdsRow->getCampaign()->getName(),
            PHP_EOL
        );
    }
}
      

Python

def main(client, customer_id):
    ga_service = client.get_service("GoogleAdsService")

    query = """
        SELECT
          campaign.id,
          campaign.name
        FROM campaign
        ORDER BY campaign.id"""

    # Issues a search request using streaming.
    stream = ga_service.search_stream(customer_id=customer_id, query=query)

    for batch in stream:
        for row in batch.results:
            print(
                f"Campaign with ID {row.campaign.id} and name "
                f'"{row.campaign.name}" was found.'
            )
      

Ruby

def get_campaigns(customer_id)
  # GoogleAdsClient will read a config file from
  # ENV['HOME']/google_ads_config.rb when called without parameters
  client = Google::Ads::GoogleAds::GoogleAdsClient.new

  responses = client.service.google_ads.search_stream(
    customer_id: customer_id,
    query: 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id',
  )

  responses.each do |response|
    response.results.each do |row|
      puts "Campaign with ID #{row.campaign.id} and name '#{row.campaign.name}' was found."
    end
  end
end
      

Perl

sub get_campaigns {
  my ($api_client, $customer_id) = @_;

  # Create a search Google Ads stream request that will retrieve all campaigns.
  my $search_stream_request =
    Google::Ads::GoogleAds::V17::Services::GoogleAdsService::SearchGoogleAdsStreamRequest
    ->new({
      customerId => $customer_id,
      query      =>
        "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id"
    });

  # Get the GoogleAdsService.
  my $google_ads_service = $api_client->GoogleAdsService();

  my $search_stream_handler =
    Google::Ads::GoogleAds::Utils::SearchStreamHandler->new({
      service => $google_ads_service,
      request => $search_stream_request
    });

  # Issue a search request and process the stream response to print the requested
  # field values for the campaign in each row.
  $search_stream_handler->process_contents(
    sub {
      my $google_ads_row = shift;
      printf "Campaign with ID %d and name '%s' was found.\n",
        $google_ads_row->{campaign}{id}, $google_ads_row->{campaign}{name};
    });

  return 1;
}
      

คุณอาจต้องทำดังนี้ ทั้งนี้ขึ้นอยู่กับกรณีการใช้งาน

  • เพิ่มประสิทธิภาพจำนวนหน้าที่มีการดึงข้อมูล
  • เพิ่มประสิทธิภาพของจำนวนผลลัพธ์ที่จัดเก็บในในแต่ละครั้ง
  • ดาวน์โหลดและประมวลผลหน้าเว็บของผลการค้นหาตามลำดับที่กำหนด

สามารถทำได้โดยการจัดเก็บโทเค็นของหน้าเว็บแทนผลลัพธ์ ซึ่งจะเพิ่ม ความซับซ้อนของโค้ดมากยิ่งขึ้น

ตัวอย่างโค้ด

ดูตัวอย่างโค้ดของฟังก์ชันทั่วไปบางรายการใน Google Ads API