Exemple #1
0
        public async Task <RecoResult> Recommendtion(string id)
        {
            var result = new RecoResult();

            using (XConnectClient client
                       = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                var reference = new ContactReference(Guid.Parse(id));

                var expandOptions = new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, ProductRecommendationFacet.DefaultFacetKey)
                {
                    Interactions = new RelatedInteractionsExpandOptions()
                };

                Task <Contact> contactTask = client.GetAsync <Contact>(reference, expandOptions);

                Contact contact = await contactTask;

                if (contact != null)
                {
                    result.FirstName = contact.Personal()?.FirstName;
                    result.LastName  = contact.Personal()?.LastName;
                    var purchaseOutcome = contact.Interactions.Select(i => i.Events.OfType <ProductPurchasedOutcome>()).FirstOrDefault();
                    result.PurchasedProductId = purchaseOutcome.FirstOrDefault().ProductId;
                    var recommendationFacet = contact.GetFacet <ProductRecommendationFacet>();
                    if (recommendationFacet != null)
                    {
                        result.Recommends = recommendationFacet.ProductRecommendations;
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        public async Task <List <RecoResult> > Recommendtions()
        {
            var result = new List <RecoResult>();

            using (XConnectClient client
                       = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                var query = client.Contacts.Where(contact =>
                                                  contact.Interactions.Any(interaction =>
                                                                           interaction.Events.OfType <ProductPurchasedOutcome>().Any() &&
                                                                           interaction.StartDateTime > DateTime.UtcNow.AddDays(-1) //Only for outcome happened in past 24 hours
                                                                           )
                                                  );

                var expandOptions = new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, ProductRecommendationFacet.DefaultFacetKey)
                {
                    Interactions = new RelatedInteractionsExpandOptions()
                };

                query = query.WithExpandOptions(expandOptions);

                var contacts = await query.ToList();

                if (contacts.Any())
                {
                    foreach (var c in contacts)
                    {
                        var purchaseOutcome     = c.Interactions.Select(i => i.Events.OfType <ProductPurchasedOutcome>()).FirstOrDefault();
                        var purchasedId         = purchaseOutcome.FirstOrDefault().ProductId;
                        var recommendationFacet = c.GetFacet <ProductRecommendationFacet>();

                        var r = new RecoResult()
                        {
                            FirstName          = c.Personal()?.FirstName,
                            LastName           = c.Personal()?.LastName,
                            PurchasedProductId = purchasedId
                        };

                        if (recommendationFacet != null)
                        {
                            r.Recommends = recommendationFacet.ProductRecommendations;
                        }

                        result.Add(r);
                    }
                }
            }
            return(result);
        }
Exemple #3
0
        public RecoResult Contact()
        {
            var result = new RecoResult();

            using (XConnectClient client
                       = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                var person    = _contactGenerator.CreateContact();
                var contactId = new ContactIdentifier(_contactsource, person.Identifier.ToString(), ContactIdentifierType.Known);
                var contact   = new Contact(contactId);

                var infoFacet = new PersonalInformation()
                {
                    FirstName = person.FirstName,
                    LastName  = person.LastName
                };
                client.SetFacet(contact, PersonalInformation.DefaultFacetKey, infoFacet);

                var emailFacet = new EmailAddressList(new EmailAddress(person.EmailAddress, true), _contactsource);
                client.SetFacet(contact, EmailAddressList.DefaultFacetKey, emailFacet);

                var purchaseOutcome = new ProductPurchasedOutcome(ProductPurchasedOutcome.CustomPurchaseOutcomeDefinitionId, DateTime.UtcNow, "AUD", 0)
                {
                    ProductId = _contactGenerator.GetProductId()
                };

                var productPurchasedinteraction = new Interaction(contact, InteractionInitiator.Contact, _channelId, _userAgent);
                productPurchasedinteraction.Events.Add(purchaseOutcome);

                client.AddContact(contact);
                client.AddInteraction(productPurchasedinteraction);

                result.FirstName          = infoFacet.FirstName;
                result.LastName           = infoFacet.LastName;
                result.PurchasedProductId = purchaseOutcome.ProductId;

                client.Submit();
            }

            return(result);
        }