Page Summary
-
This content provides code samples in Java, PHP, and Python demonstrating how to create a Promotion DataSource using the Merchant API.
-
Promotion DataSources are designed for specific combinations of
targetCountryandcontentLanguage, as wildcards are not supported. -
The code examples use Google's client libraries to authenticate, build a
CreateDataSourceRequest, and send a request to create theDataSource. -
Each code sample demonstrates how to set the display name, target country, and content language for the promotion data source, before sending it to the API.
-
The provided code is directly tied to the Merchant API and its functions related to creating data sources, with clear instructions and comments within each example to help understanding.
Merchant API code sample to create a promotion data source.
Java
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package
shopping.merchant.samples.datasources.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.datasources.v1.CreateDataSourceRequest
;
import
com.google.shopping.merchant.datasources.v1.DataSource
;
import
com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient
;
import
com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings
;
import
com.google.shopping.merchant.datasources.v1.PromotionDataSource
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to create a promotion datasource. */
public
class
CreatePromotionDataSourceSample
{
private
static
String
getParent
(
String
merchantId
)
{
return
String
.
format
(
"accounts/%s"
,
merchantId
);
}
public
static
String
createDataSource
(
Config
config
,
String
displayName
)
throws
Exception
{
GoogleCredentials
credential
=
new
Authenticator
().
authenticate
();
DataSourcesServiceSettings
dataSourcesServiceSettings
=
DataSourcesServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
String
parent
=
getParent
(
config
.
getAccountId
().
toString
());
try
(
DataSourcesServiceClient
dataSourcesServiceClient
=
DataSourcesServiceClient
.
create
(
dataSourcesServiceSettings
))
{
CreateDataSourceRequest
request
=
CreateDataSourceRequest
.
newBuilder
()
.
setParent
(
parent
)
.
setDataSource
(
DataSource
.
newBuilder
()
.
setDisplayName
(
displayName
)
// PromotionDataSources
// can only be created for a specific `targetCountry` and `contentLanguage`
// combination.
.
setPromotionDataSource
(
PromotionDataSource
.
newBuilder
()
.
setContentLanguage
(
"en"
)
.
setTargetCountry
(
"GB"
)
.
build
())
.
build
())
.
build
();
System
.
out
.
println
(
"Sending Create Promotion DataSource request"
);
DataSource
response
=
dataSourcesServiceClient
.
createDataSource
(
request
);
System
.
out
.
println
(
"Inserted DataSource Name below"
);
System
.
out
.
println
(
response
.
getName
());
return
response
.
getName
();
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
System
.
exit
(
1
);
return
null
;
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Config
config
=
Config
.
load
();
// The displayed datasource name in the Merchant Center UI.
String
displayName
=
"British Promotions"
;
createDataSource
(
config
,
displayName
);
}
}
PHP
< ?php
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
require_once __DIR__ . '/../../Authentication/Authentication.php';
require_once __DIR__ . '/../../Authentication/Config.php';
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\DataSources\V1\Client\DataSourcesServiceClient;
use Google\Shopping\Merchant\DataSources\V1\CreateDataSourceRequest;
use Google\Shopping\Merchant\DataSources\V1\DataSource;
use Google\Shopping\Merchant\DataSources\V1\PromotionDataSource;
/**
* This class demonstrates how to create a promotion datasource.
*/
class CreatePromotionDataSource
{
/**
* Creates a new PromotionDataSource.
*
* @param int $merchantId The Merchant Center account ID.
* @param string $displayName The displayed datasource name in the Merchant Center UI.
* @return string The name of the newly created PromotionDataSource.
*/
function createPromotionDataSourceSample(int $merchantId, string $displayName): string
{
// Gets the OAuth credentials to make the request.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates options config containing credentials for the client to use.
$options = ['credentials' => $credentials];
// Creates a client.
$dataSourcesServiceClient = new DataSourcesServiceClient($options);
$parent = sprintf('accounts/%s', $merchantId);
// Creates the data source.
$dataSource = (new DataSource())
->setDisplayName($displayName)
->setPromotionDataSource(
(new PromotionDataSource())
->setContentLanguage('en')
->setTargetCountry('GB')
);
// Creates the request.
$request = (new CreateDataSourceRequest())
->setParent($parent)
->setDataSource($dataSource);
// Sends the request to the API.
try {
print('Sending Create Promotion DataSource request' . PHP_EOL);
$response = $dataSourcesServiceClient->createDataSource($request);
print('Inserted DataSource Name below' . PHP_EOL);
print($response->getName() . PHP_EOL);
return $response->getName();
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
return '';
}
}
// Helper to execute the sample.
public function callSample(): void
{
$config = Config::generateConfig();
// The Merchant Center Account ID.
$merchantId = $config['accountId'];
// The displayed datasource name in the Merchant Center UI.
$displayName = 'British Promotions';
$this->createPromotionDataSourceSample($merchantId, $displayName);
}
}
$sample = new CreatePromotionDataSource();
$sample->callSample();

