I've tried this code, taken almost directly from sitecore documentation (they are using Goal, I'm using PageViewEvent).

var itemId = <some id of an Item in content>;
            using (var client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
                queryable = client.Interactions.Where(x =>
                    x.Events.OfType<Sitecore.XConnect.Collection.Model.PageViewEvent>().Any(y =>
                        y.ItemId == itemId.Guid));

                if (queryable == null) return totalCount;

                Sitecore.XConnect.Client.Synchronous.IEntityBatchEnumerator<Interaction> enumerable =

                while (enumerable.MoveNext())
                    foreach (Interaction interaction in enumerable.Current)
                        int pageVisits = interaction.Events.OfType<Sitecore.XConnect.Collection.Model.PageViewEvent>()
                            .Count(x =>
                                x.ItemId == itemId.Guid &&
                                x.ItemLanguage == Sitecore.Context.Language.Name);

                        if (uniqueVisits)
                            pageVisits = pageVisits > 0 ? 1 : 0;

                        totalCount += pageVisits;

This always returns for totalCount. On closer inspection, I found that the actual interactions are 0 (i.e. client.Interactions even without filtering, returns a totalCount of 0).

I check the analytics reports and I see page visits, even to the particular item I'm using, so it appears that data exists.

Any idea why this is happening, and/or what I'm doing wrong here?


This should work for you.

IAsyncQueryable<Interaction> queryable = client.Interactions
    .Where(x => x.Events.Any(y => y.DefinitionId == Sitecore.XConnect.Collection.Model.PageViewEvent.EventDefinitionId))
    //.WithExpandOptions(new InteractionExpandOptions(new string[] { IpInfo.DefaultFacetKey })
        //Contact = new RelatedContactExpandOptions(TrialFacet.DefaultFacetKey)

var enumerable = queryable.GetBatchEnumeratorSync(200);

I added the contact facet code (remmed out) just in case you need to get contact Facets also.

  • Hello Chris. I don't see how this will help me. As stated, client.Interactions returns 0 results, so your code will also return 0 results (it's a "Where" clause on a collection with 0 results). Unless I'm missing something here. Dec 4 '21 at 16:22
  • I have always had trouble with the OfType<> searches. Mine goes off it DefinitionId. Works better for me.
    – Chris Auer
    Dec 6 '21 at 18:06
  • I can understand that, but my issue is not on the where clause. My issue is that the following: using (var client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { queryable = client.Interactions } returns an empty set, ie it appears as though there are no interactions (while clearly there are, because they appear within sitecore analytics) Dec 7 '21 at 9:27

You need to add user interaction (either event or goal) before submitting the contact to XDB.

I am writing below a sample code to save interaction:

public virtual ContactIdentifier CreateContact(string email, PersonalInformation personalInfo)
        var identifier = new ContactIdentifier(ContactSource, email, ContactIdentifierType.Known);
        var identifiers = new ContactIdentifier[] { identifier };

            if (CurrentTracker != null && CurrentTracker.IsActive && CurrentTracker.Session != null)
                CurrentTracker.Session.IdentifyAs(ContactSource, email);
                CurrentTracker.Contact.Tags.Set("isEmpty", "false");

            using (var client = SitecoreXConnectClientConfiguration.GetClient())
                var knownContact = new Contact(identifiers);
                client.SetFacet(knownContact, PersonalInformation.DefaultFacetKey, personalInfo);

                // Add User interaction before submit contact
                Guid channelId = Guid.Parse("27A7E0C2-DE17-46C8-8AA3-CFEC0434CCEB");
                var interaction = new Sitecore.XConnect.Interaction(knownContact, InteractionInitiator.Brand, channelId, "userAgent");
                var fakeItemID = Guid.Parse("1D5C266A-112F-4EA2-A69E-E4865ACE2200"); // Replace with real item ID
                PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, 3, "en")
                    Duration = new TimeSpan(0, 0, 30),
                    Url = "/Home"

        catch (XdbExecutionException ex)
            Log.Error("Exception creating contact", ex, this);
        return identifier;
  • Hello Himmat. I'm not trying to register an interaction, I'm only trying to retrieve the number of pageview events for a specific page. The ultimate goal is to get the "most popular" pages. Dec 4 '21 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.