Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 84 additions & 8 deletions samples/audiences/ingest_audience_members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import {IngestionServiceClient} from '@google-ads/datamanager';
import {protos} from '@google-ads/datamanager';
const {
AudienceMember,
CompositeData,
Comment thread
jradcliff marked this conversation as resolved.
Destination,
Encoding: DataManagerEncoding,
Consent,
ConsentStatus,
IngestAudienceMembersRequest,
IpData,
ProductAccount,
TermsOfService,
TermsOfServiceStatus,
Expand Down Expand Up @@ -52,6 +54,11 @@ interface Arguments {
interface MemberRow {
emails?: string[];
phoneNumbers?: string[];
ipInfos?: {
ipAddress: string;
observeStartTime?: string;
observeEndTime?: string;
}[];
}

/**
Expand Down Expand Up @@ -132,6 +139,13 @@ async function main() {

const memberRows: MemberRow[] = await readMemberData(argv.json_file);

// Converts the operating account type argument to the corresponding enum
// so IP address checks can compare enums to enums.
const operatingAccountType = convertToAccountType(
argv.operating_account_type,
'operating_account_type',
);

// Builds the audience_members collection for the request.
const audienceMembers = [];
for (const memberRow of memberRows) {
Expand Down Expand Up @@ -167,19 +181,81 @@ async function main() {
}
}

if (userData.userIdentifiers.length > 0) {
audienceMembers.push(AudienceMember.create({userData: userData}));
// Process IP address information
const ipDatas = [];
for (const ipInfo of memberRow.ipInfos || []) {
if (operatingAccountType !== ProductAccount.AccountType.GOOGLE_ADS) {
console.log(
`Skipping IP address information for operating account type ${operatingAccountType}. ` +
'Sending IP address is only supported for operating account type GOOGLE_ADS.',
);
}

const ipAddress = (ipInfo.ipAddress || '').trim();
if (!ipAddress) {
console.log('Skipping IP address information with no IP address');
continue;
}

const ipData = IpData.create({ipAddress});

if (ipInfo.observeStartTime) {
const startTimeStr = ipInfo.observeStartTime.trim();
if (startTimeStr) {
try {
const date = new Date(startTimeStr);
if (isNaN(date.getTime())) {
throw new Error('Invalid date');
}
ipData.observeStartTime = {
seconds: Math.floor(date.getTime() / 1000),
nanos: (date.getTime() % 1000) * 1e6,
};
} catch (e) {
console.log(
`Ignoring observe start time '${startTimeStr}' since it can't be parsed`,
);
}
}
}

if (ipInfo.observeEndTime) {
const endTimeStr = ipInfo.observeEndTime.trim();
if (endTimeStr) {
try {
const date = new Date(endTimeStr);
if (isNaN(date.getTime())) {
throw new Error('Invalid date');
}
ipData.observeEndTime = {
seconds: Math.floor(date.getTime() / 1000),
nanos: (date.getTime() % 1000) * 1e6,
};
} catch (e) {
console.log(
`Ignoring observe end time '${endTimeStr}' since it can't be parsed`,
);
}
}
}

ipDatas.push(ipData);
}

if (userData.userIdentifiers.length > 0 || ipDatas.length > 0) {
const compositeData = CompositeData.create();
if (userData.userIdentifiers.length > 0) {
compositeData.userData = userData;
}
if (ipDatas.length > 0) {
compositeData.ipData = ipDatas;
}
audienceMembers.push(AudienceMember.create({compositeData}));
} else {
console.warn('Ignoring line. No data.');
}
}

// Sets up the Destination.
const operatingAccountType = convertToAccountType(
argv.operating_account_type,
'operating_account_type',
);

const destination = Destination.create({
operatingAccount: ProductAccount.create({
accountType: operatingAccountType,
Expand Down
2 changes: 1 addition & 1 deletion samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"ingest-audience-members": "ts-node audiences/ingest_audience_members.ts"
},
"dependencies": {
"@google-ads/datamanager": "^0.1.0",
"@google-ads/datamanager": "^0",
"@google-ads/data-manager-util": "^0.3.0",
"csv-parser": "^3.0.0",
"yargs": "^17.7.2"
Expand Down
15 changes: 15 additions & 0 deletions samples/sampledata/audience_members_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"emails": [
"dana@example.com",
"DanaM@example.com"
],
"ipInfos": [
{
"ipAddress": "192.0.2.211"
},
{
"ipAddress": "198.51.100.14"
}
]
},
{
Expand All @@ -21,6 +29,13 @@
"emails": [
"quinn@CYMBALGROUP.com",
"baklavainthebalkans@gmail.com"
],
"ipInfos": [
{
"ipAddress": "203.0.113.98",
"observeStartTime": "2026-06-10T20:17:52.0-04:00",
"observeEndTime": "2026-06-17T04:02:04.123-04:00"
}
]
},
{
Expand Down