private int CalculateTotalClicks()
    {
        var results = ClickedLinkInfoProvider.GetClickedLinks().Source(s => s.Join <LinkInfo>("ClickedLinkNewsletterLinkID", "LinkId"))
                      .WhereIn("LinkIssueID", mParentAndVariantsIDs);

        return(results.Count);
    }
    private int CalculateUniqueClicks()
    {
        var results = ClickedLinkInfoProvider.GetClickedLinks().Source(s => s.Join <LinkInfo>("ClickedLinkNewsletterLinkID", "LinkId"))
                      .WhereIn("LinkIssueID", mParentAndVariantsIDs)
                      .Distinct().Columns("ClickedLinkEmail", "LinkIssueID");

        return(results.Count);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        mNewsletter = EditedObjectParent as NewsletterInfo;

        if (mNewsletter == null)
        {
            RedirectToAccessDenied(GetString("general.invalidparameters"));
        }

        if (!mNewsletter.CheckPermissions(PermissionsEnum.Read, CurrentSiteName, CurrentUser))
        {
            RedirectToAccessDenied(mNewsletter.TypeInfo.ModuleName, "Read");
        }

        mBounceMonitoringEnabled = NewsletterHelper.MonitorBouncedEmails(CurrentSiteName);
        mTrackingEnabled         = NewsletterHelper.IsTrackingAvailable();

        if (mTrackingEnabled && !mBounceMonitoringEnabled)
        {
            ShowInformation(GetString("newsletter.viewadditionalstatsmessage"));
        }

        ScriptHelper.RegisterTooltip(this);

        // Initialize unigrid
        UniGrid.WhereCondition       = String.Format("IssueNewsletterID={0} AND IssueVariantOfIssueID IS NULL", mNewsletter.NewsletterID);
        UniGrid.ZeroRowsText         = GetString("Newsletter_Issue_List.NoIssuesFound");
        UniGrid.OnAction            += uniGrid_OnAction;
        UniGrid.OnBeforeDataReload  += UniGrid_OnBeforeDataReload;
        UniGrid.OnExternalDataBound += UniGrid_OnExternalDataBound;

        // Initialize header actions
        InitHeaderActions();

        // Prepare data for listing
        // Group clicked link records by IssueID with Columns LinkIssueID, UniqueUserClicks, VariantParent (to calculate clicked links through all variant)
        mClickedLinksSummary = ClickedLinkInfoProvider.GetClickedLinks()
                               .Columns(new QueryColumn("LinkIssueID"),
                                        new AggregatedColumn(AggregationType.Count, "DISTINCT(ClickedLinkEmail)").As("UniqueUserClicks"))
                               .Source(s => s.Join <LinkInfo>("ClickedLinkNewsletterLinkID", "LinkID"))
                               .GroupBy("LinkIssueID");

        // Prepare variant summaries
        mVariantIssueSummaries = IssueInfoProvider.GetIssues()
                                 .Columns(new QueryColumn("IssueVariantOfIssueID"),
                                          new AggregatedColumn(AggregationType.Sum, "IssueOpenedEmails").As("OpenedEmailsSum"))
                                 .WhereEquals("IssueNewsletterID", mNewsletter.NewsletterID)
                                 .GroupBy("IssueVariantOfIssueID")
                                 .Having("IssueVariantOfIssueID IS NOT NULL");

        // AB Variant issues for current newsletter
        mVariantIssues = IssueInfoProvider.GetIssues()
                         .Columns("IssueID", "IssueVariantOfIssueID")
                         .WhereEquals("IssueNewsletterID", mNewsletter.NewsletterID)
                         .WhereNotNull("IssueVariantOfIssueID");

        AddBrokenEmailUrlNotifier(mNewsletter, lblUrlWarning);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        PageTitle.TitleText = GetString("newsletter_issue_subscribersclicks.title");
        linkId = QueryHelper.GetInteger("linkid", 0);
        if (linkId == 0)
        {
            RequestHelper.EndResponse();
        }

        LinkInfo link = LinkInfoProvider.GetLinkInfo(linkId);

        EditedObject = link;

        IssueInfo issue = IssueInfoProvider.GetIssueInfo(link.LinkIssueID);

        EditedObject = issue;

        // Prevent accessing issues from sites other than current site
        if (issue.IssueSiteID != SiteContext.CurrentSiteID)
        {
            RedirectToResourceNotAvailableOnSite("Issue with ID " + link.LinkIssueID);
        }

        var listingWhereCondition = new WhereCondition().WhereEquals("ClickedLinkNewsletterLinkID", linkId);

        // Link's issue is the main A/B test issue
        if (issue.IssueIsABTest && !issue.IssueIsVariant)
        {
            // Get A/B test and its winner issue ID
            ABTestInfo test = ABTestInfoProvider.GetABTestInfoForIssue(issue.IssueID);
            if (test != null)
            {
                // Get ID of the same link from winner issue
                var winnerLink = LinkInfoProvider.GetLinks()
                                 .WhereEquals("LinkIssueID", test.TestWinnerIssueID)
                                 .WhereEquals("LinkTarget", link.LinkTarget)
                                 .WhereEquals("LinkDescription", link.LinkDescription)
                                 .TopN(1)
                                 .Column("LinkID")
                                 .FirstOrDefault();

                if (winnerLink != null)
                {
                    if (winnerLink.LinkID > 0)
                    {
                        // Add link ID of winner issue link
                        listingWhereCondition.Or(new WhereCondition().WhereEquals("ClickedLinkNewsletterLinkID", winnerLink.LinkID));
                    }
                }
            }
        }

        UniGrid.Pager.DefaultPageSize = PAGESIZE;
        UniGrid.Pager.ShowPageSize    = false;
        UniGrid.FilterLimit           = 1;
        fltOpenedBy.EmailColumn       = "ClickedLinkEmail";

        // Get click count by email
        UniGrid.DataSource = ClickedLinkInfoProvider.GetClickedLinks()
                             .Columns(
            new QueryColumn("ClickedLinkEmail"),
            new AggregatedColumn(AggregationType.Count, null).As("ClickCount")
            )
                             .GroupBy("ClickedLinkEmail")
                             .Where(listingWhereCondition)
                             .And()
                             .Where(fltOpenedBy.WhereCondition)
                             .OrderByDescending("ClickCount")
                             .Result;
    }