Amazon Pinpoint C# SDK

0

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);
    }
}
1 Answer
0

Hello,

After going through the code logic, I noticed that you have created one activity for the journey but no where in that activity you have mentioned the segment that needs to be used in the journey. Moreover, in the WriteJourneyRequest, you are not using the created activity anywhere and in the startCondition, you are trying to match the value of two segment ID variables but in the journey definition, there is no value for segment which is being used in the journey so you are comparing a null value with segment ID of the segment created above, which might be resulting in the behaviour you are observing. Please refer the below CLI definition for journey creation for reference:

[+] https://docs.aws.amazon.com/cli/latest/reference/pinpoint/create-journey.html

The above being said, since this is code debugging I would suggest you to get in touch with sales team of AWS and they will help you with assigning the best possible team that can assist you with your use case.

[+] https://aws.amazon.com/contact-us/aws-sales/

AWS
Tanya_G
answered 8 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions