Hello,
I am trying to use the C# AWS SDK to create Pinpoint Segments and Journey.
Sometimes when I execute the code with the Journey status as DRAFT, It creates both but in the Journey, it says "Segment no longer exists" and sometimes it adds the segment, but the Total endpoints in segments are zero.
When I execute with the Journey status as ACTIVE, it creates, but it doesn't get the contacts from the Segment.
Segment without contacts
If I try to create it manually, it works fine, only by using the code it doesn't do everything.
My C# .netFramework 4.8 code:
public ActionResult CreateSegment()
{
Amazon.RegionEndpoint region = Amazon.RegionEndpoint.USEast1;
string bucketName = ConfigurationManager.AppSettings["BucketName"];
string accessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
string secretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
var pinpointAppId = ConfigurationManager.AppSettings["PinPointAppId"];
// Pinpoint client and segment definition logic
var pinpointClient = new Amazon.Pinpoint.AmazonPinpointClient(accessKey, secretKey, region);
var segmentName = "Segment_wa_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
var journeyName = "Journey_wa_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
// Define the import job details
var importJobRequest = new Amazon.Pinpoint.Model.CreateImportJobRequest
{
ApplicationId = pinpointAppId,
ImportJobRequest = new Amazon.Pinpoint.Model.ImportJobRequest
{
DefineSegment = true,
Format = Amazon.Pinpoint.Format.CSV,
RegisterEndpoints = true,
RoleArn = "ROLE_ARN",
S3Url = $"s3://amazon-pinpoint-segment-files/Staging/Pinpoint-WallaceTest.csv",
SegmentName = segmentName
}
};
// Create the import job
var createImportJobResponse = pinpointClient.CreateImportJobAsync(importJobRequest).Result;
// Check for successful import job creation
if (createImportJobResponse.HttpStatusCode == HttpStatusCode.OK || createImportJobResponse.HttpStatusCode == HttpStatusCode.Created)
{
if (createImportJobResponse.ImportJobResponse != null && createImportJobResponse.ImportJobResponse.Definition != null)
{
Console.WriteLine("Import job created successfully. Job ID: " + createImportJobResponse.ImportJobResponse.Id);
var segmentId = createImportJobResponse.ImportJobResponse.Definition.SegmentId;
// Check the Segment Jobs status
var getImportRequest = new Amazon.Pinpoint.Model.GetImportJobRequest()
{
ApplicationId = pinpointAppId,
JobId = createImportJobResponse.ImportJobResponse.Id
};
var numberOfTries = 100;
var count = 0;
var jobSuccess = false;
while (!jobSuccess)
{
try
{
if (count > numberOfTries)
break;
var segmentResponse = pinpointClient.GetImportJob(getImportRequest);
count++;
Thread.Sleep(2000);
if (segmentResponse.ImportJobResponse != null)
{
if (segmentResponse.ImportJobResponse.JobStatus == Amazon.Pinpoint.JobStatus.COMPLETED)
{
jobSuccess = true;
break;
}
else if (segmentResponse.ImportJobResponse.JobStatus == Amazon.Pinpoint.JobStatus.FAILING ||
segmentResponse.ImportJobResponse.JobStatus == Amazon.Pinpoint.JobStatus.FAILED)
{
jobSuccess = false;
break;
}
}
}
catch (Exception)
{ }
}
if (jobSuccess)
{
Dictionary<string, Amazon.Pinpoint.Model.Activity> activities = new Dictionary<string, Amazon.Pinpoint.Model.Activity>();
// Define the journey activities
activities.Add("ConnectCall", new Amazon.Pinpoint.Model.Activity()
{
ContactCenter = new Amazon.Pinpoint.Model.ContactCenterActivity() { }
});
// Define the journey definition
var createJourneyRequest = new Amazon.Pinpoint.Model.CreateJourneyRequest
{
ApplicationId = pinpointAppId,
WriteJourneyRequest = new Amazon.Pinpoint.Model.WriteJourneyRequest()
{
Name = journeyName,
Schedule = new Amazon.Pinpoint.Model.JourneySchedule()
{
StartTime = DateTime.Now.AddMinutes(2),
EndTime = DateTime.Now.AddMinutes(12),
Timezone = "UTC-07"
},
StartCondition = new Amazon.Pinpoint.Model.StartCondition
{
SegmentStartCondition = new Amazon.Pinpoint.Model.SegmentCondition
{
SegmentId = segmentId
}
},
State = Amazon.Pinpoint.State.ACTIVE,
JourneyChannelSettings = new Amazon.Pinpoint.Model.JourneyChannelSettings
{
ConnectCampaignArn = "AMAZON_CONNECT_CAMPAIGN_ARN",
ConnectCampaignExecutionRoleArn = "ROLE_ARN"
},
StartActivity = "ConnectCall",
LocalTime = true,
Activities = activities,
RefreshOnSegmentUpdate = false
}
};
// Create the journey
var journey = pinpointClient.CreateJourney(createJourneyRequest);
if (journey.HttpStatusCode == HttpStatusCode.OK || journey.HttpStatusCode == HttpStatusCode.Created)
{
// Future logic
}
}
}
}
else
{
Console.WriteLine("Error creating import job: " + createImportJobResponse.HttpStatusCode);
}
}