Skip to content

Commit

Permalink
Added custom key variant of UploadStoreSalesTransactions (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewMBurke authored Aug 25, 2020
1 parent 3cedaab commit 3b6cfde
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions examples/Remarketing/UploadStoreSalesTransactions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class UploadStoreSalesTransactions : ExampleBase
// method.
private static readonly SHA256 _digest = SHA256.Create();

// If uploading data with custom key and values, specify the value:
private const string CUSTOM_VALUE = "INSERT_CUSTOM_VALUE_HERE";

/// <summary>
/// Main method, to run this code example as a standalone application.
/// </summary>
Expand Down Expand Up @@ -69,13 +72,16 @@ public static void Main(string[] args)
string bridgeMapVersionId = "INSERT_BRIDGE_MAP_VERSION_ID_HERE";
long? partnerId = long.Parse("INSERT_PARTNER_ID_HERE");

// OPTIONAL: If uploading data with custom key and values, specify the key:
string customKey = "INSERT_CUSTOM_KEY_HERE";

codeExample.Run(new GoogleAdsClient(), customerId, conversionActionId,
offlineUserDataJobType: offlineUserDataJobType,
externalId: externalId,
advertiserUploadDateTime: advertiserUploadDateTime,
bridgeMapVersionId: bridgeMapVersionId,
partnerId: partnerId);
partnerId: partnerId,
customKey: customKey);
}

/// <summary>
Expand Down Expand Up @@ -104,11 +110,13 @@ public static void Main(string[] args)
/// required if uploading third party data.</param>
/// <param name="partnerId">ID of the third party partner. Only required if uploading third
/// party data.</param>
/// <param name="customKey">Optional custom key name. Only required if uploading data
/// with custom key and values.</param>
public void Run(GoogleAdsClient client, long customerId, long conversionActionId,
OfflineUserDataJobType offlineUserDataJobType =
OfflineUserDataJobType.StoreSalesUploadFirstParty,
long? externalId = null, string advertiserUploadDateTime = null,
string bridgeMapVersionId = null, long? partnerId = null)
string bridgeMapVersionId = null, long? partnerId = null, string customKey=null)
{
// Get the OfflineUserDataJobServiceClient.
OfflineUserDataJobServiceClient offlineUserDataJobServiceClient =
Expand All @@ -128,18 +136,18 @@ public void Run(GoogleAdsClient client, long customerId, long conversionActionId
string offlineUserDataJobResourceName =
CreateOfflineUserDataJob(offlineUserDataJobServiceClient, customerId,
offlineUserDataJobType, externalId, advertiserUploadDateTime,
bridgeMapVersionId, partnerId);
bridgeMapVersionId, partnerId, customKey);

// Adds transactions to the job.
AddTransactionsToOfflineUserDataJob(offlineUserDataJobServiceClient, customerId,
offlineUserDataJobResourceName, conversionActionId);
offlineUserDataJobResourceName, conversionActionId, customKey);

// Issues an asynchronous request to run the offline user data job.
offlineUserDataJobServiceClient.RunOfflineUserDataJobAsync(
offlineUserDataJobResourceName);

Console.WriteLine("Sent request to asynchronously run offline user data job " +
$"{offlineUserDataJobResourceName}.");
$"{offlineUserDataJobResourceName}.");

// Offline user data jobs may take up to 24 hours to complete, so instead of waiting
// for the job to complete, retrieves and displays the job status once and then
Expand Down Expand Up @@ -174,11 +182,14 @@ public void Run(GoogleAdsClient client, long customerId, long conversionActionId
/// required if uploading third party data.</param>
/// <param name="partnerId">ID of the third party partner. Only required if uploading third
/// party data.</param>
/// <param name="customKey">The custom key, or null if not uploading data with custom key
/// and value.</param>
/// <returns>The resource name of the created job.</returns>
private string CreateOfflineUserDataJob(
OfflineUserDataJobServiceClient offlineUserDataJobServiceClient, long customerId,
OfflineUserDataJobType offlineUserDataJobType, long? externalId,
string advertiserUploadDateTime, string bridgeMapVersionId, long? partnerId)
string advertiserUploadDateTime, string bridgeMapVersionId, long? partnerId,
string customKey)
{
// TIP: If you are migrating from the AdWords API, please note that Google Ads API uses
// the term "fraction" instead of "rate". For example, loyaltyRate in the AdWords API is
Expand All @@ -204,6 +215,12 @@ private string CreateOfflineUserDataJob(
TransactionUploadFraction = 1.0
};

// Apply the custom key if provided.
if (!string.IsNullOrEmpty(customKey))
{
storeSalesMetadata.CustomKey = customKey;
}

// Creates additional metadata required for uploading third party data.
if (offlineUserDataJobType == OfflineUserDataJobType.StoreSalesUploadThirdParty)
{
Expand Down Expand Up @@ -266,7 +283,7 @@ private string CreateOfflineUserDataJob(
customerId.ToString(), offlineUserDataJob);
string offlineUserDataJobResourceName = createOfflineUserDataJobResponse.ResourceName;
Console.WriteLine("Created an offline user data job with resource name: " +
$"{offlineUserDataJobResourceName}.");
$"{offlineUserDataJobResourceName}.");
return offlineUserDataJobResourceName;
}

Expand All @@ -279,13 +296,15 @@ private string CreateOfflineUserDataJob(
/// <param name="offlineUserDataJobResourceName">The resource name of the job to which to
/// add transactions.</param>
/// <param name="conversionActionId">The ID of a store sales conversion action.</param>
/// <param name="customKey">The custom key, or null if not uploading data with custom key
/// and value.</param>
private void AddTransactionsToOfflineUserDataJob(
OfflineUserDataJobServiceClient offlineUserDataJobServiceClient, long customerId,
string offlineUserDataJobResourceName, long conversionActionId)
string offlineUserDataJobResourceName, long conversionActionId, string customKey)
{
// Constructions an operation for each transaction.
List<OfflineUserDataJobOperation> userDataJobOperations =
BuildOfflineUserDataJobOperations(customerId, conversionActionId);
BuildOfflineUserDataJobOperations(customerId, conversionActionId, customKey);

// Issues a request with partial failure enabled to add the operations to the offline
// user data job.
Expand Down Expand Up @@ -320,9 +339,11 @@ private void AddTransactionsToOfflineUserDataJob(
/// </summary>
/// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
/// <param name="conversionActionId">The ID of a store sales conversion action.</param>
/// <param name="customKey">The custom key, or null if not uploading data with custom key
/// and value.</param>
/// <returns>A list of operations.</returns>
private List<OfflineUserDataJobOperation> BuildOfflineUserDataJobOperations(long customerId,
long conversionActionId)
long conversionActionId, string customKey)
{
// Create the first transaction for upload based on an email address and state.
UserData userDataWithEmailAddress = new UserData()
Expand Down Expand Up @@ -357,6 +378,12 @@ private List<OfflineUserDataJobOperation> BuildOfflineUserDataJobOperations(long
}
};

// Set the custom value if a custom key was provided.
if (!string.IsNullOrEmpty(customKey))
{
userDataWithEmailAddress.TransactionAttribute.CustomValue = CUSTOM_VALUE;
}

// Creates the second transaction for upload based on a physical address.
UserData userDataWithPhysicalAddress = new UserData()
{
Expand Down

0 comments on commit 3b6cfde

Please sign in to comment.