รหัส
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เอนทิตี Google Ads ส่วนใหญ่แสดงgetId()
เมธอดที่แสดงตัวระบุ
ของเอนทิตี แม้ว่าในกรณีส่วนใหญ่จะไม่จำเป็นอย่างเคร่งครัด แต่รหัสอาจมีประโยชน์
เมื่อ
- การทำงานกับรายงาน
- รหัสเป็นวิธีที่ดีในการลิงก์แถวรายงานกับเอนทิตี Google Ads จริง
- การรักษาการแมปกับที่เก็บข้อมูลภายนอก
- คุณอาจมีข้อมูลที่อิงตามรหัสซึ่งจัดเก็บไว้ในฐานข้อมูลของคุณเองอยู่แล้ว
- หากต้องการเพิ่มประสิทธิภาพเล็กน้อย
การดึงข้อมูลตามรหัสมักจะเร็วกว่าวิธีอื่นๆ โค้ดสำหรับการดึงข้อมูล
เอนทิตีเดียวก็ง่ายขึ้นเล็กน้อยเช่นกัน
let campaigns = AdsApp.campaigns()
.withIds([678678])
.get();
// vs.
let campaigns = AdsApp.campaigns()
.withCondition("Name='My Campaign'")
.get();
ความเป็นเอกลักษณ์
รหัสแคมเปญและรหัสกลุ่มโฆษณาจะไม่ซ้ำกัน โดยไม่มีแคมเปญหรือกลุ่มโฆษณา 2 รายการที่ใช้รหัสเดียวกัน
อย่างไรก็ตาม โฆษณาและคีย์เวิร์ดมีรหัสแบบผสม โดยตัวระบุที่ไม่ซ้ำกันของคีย์เวิร์ดคือการรวมกันของรหัสกลุ่มโฆษณาและรหัสคีย์เวิร์ด
ในทำนองเดียวกัน ตัวระบุที่ไม่ซ้ำกันของโฆษณาคือการรวมกันของรหัสกลุ่มโฆษณาและรหัสโฆษณา ซึ่งจะส่งผลต่อวิธีเรียกใช้ selector.withIds()
สําหรับแคมเปญและกลุ่มโฆษณา selector.withIds()
คาดหวังอาร์เรย์ของตัวเลขดังนี้
let ids = [123123, 234234, 345345];
let campaignSelector = AdsApp.campaigns().withIds(ids);
แต่สำหรับโฆษณาและคีย์เวิร์ด selector.withIds()
ต้องมีอาร์เรย์ของอาร์เรย์ 2 องค์ประกอบ โดยองค์ประกอบแรกคือรหัสกลุ่มโฆษณา ข้อมูลโค้ดต่อไปนี้
ดึงคีย์เวิร์ด 3 รายการจากกลุ่มโฆษณา
let adGroupId = 123123;
let keywordSelector = AdsApp.keywords().withIds([
[adGroupId, 234234],
[adGroupId, 345345],
[adGroupId, 456456]
]);
โครงสร้างเดียวกันนี้จะใช้เมื่อดึงข้อมูลโฆษณา
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-27 UTC
[null,null,["อัปเดตล่าสุด 2025-08-27 UTC"],[[["\u003cp\u003eMost Google Ads entities have a \u003ccode\u003egetId()\u003c/code\u003e method, which returns a unique identifier that can be useful for linking data, improving performance, and referencing external databases.\u003c/p\u003e\n"],["\u003cp\u003eWhen working with reports, IDs can connect report rows to specific Google Ads entities.\u003c/p\u003e\n"],["\u003cp\u003eFetching entities by ID is often faster than using other methods like filtering by name.\u003c/p\u003e\n"],["\u003cp\u003eCampaign and ad group IDs are unique, while ad and keyword IDs are composite, requiring both the ad group ID and their individual ID for unique identification.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eselector.withIds()\u003c/code\u003e method is used to fetch entities by ID, taking an array of numbers for campaigns and ad groups and an array of two-element arrays (ad group ID and entity ID) for ads and keywords.\u003c/p\u003e\n"]]],[],null,["# IDs\n\nMost Google Ads entities expose a `getId()` method that returns their\nidentifier. While not strictly necessary in most cases, IDs may come in handy\nwhen\n\nWorking with [reports](/google-ads/scripts/docs/features/reports)\n: IDs provide a good way to link a report row to the actual Google Ads entity.\n\nMaintaining a mapping with an external data store\n: You may already have ID-based information stored in your own database.\n\nLooking for a bit of a performance boost\n\n: Fetching by IDs is often quicker than alternatives. The code for fetching a\n single entity is a bit easier too:\n\n let campaigns = AdsApp.campaigns()\n .withIds([678678])\n .get();\n // vs.\n let campaigns = AdsApp.campaigns()\n .withCondition(\"Name='My Campaign'\")\n .get();\n\nUniqueness\n----------\n\nCampaign IDs and ad group IDs are unique: no two campaigns or ad groups will\never share the same ID. Ads and keywords, however, have composite IDs: a unique\nidentifier of a keyword is a combination of its ad group ID and keyword ID.\nLikewise, a unique identifier of an ad is a combination of its ad group ID and\nad ID. This has implications for the way `selector.withIds()` is called.\n\nFor campaigns and ad groups, `selector.withIds()` expects an array of numbers: \n\n let ids = [123123, 234234, 345345];\n let campaignSelector = AdsApp.campaigns().withIds(ids);\n\nFor ads and keywords, however, `selector.withIds()` needs an array of\ntwo-element arrays, the first element being the ad group ID. The following\nsnippet retrieves three keywords from an ad group: \n\n let adGroupId = 123123;\n let keywordSelector = AdsApp.keywords().withIds([\n [adGroupId, 234234],\n [adGroupId, 345345],\n [adGroupId, 456456]\n ]);\n\nThe same construct applies when fetching ads."]]