public async Task <HtmlString> InvokeAsync()
        {
            AgilityContext.HttpContext = HttpContext;

            AgilityPage currentPage = AgilityContext.Page;

            if (currentPage == null)
            {
                return(null);
            }

            StringBuilder sb = new StringBuilder(Environment.NewLine);

            string scriptTopGlobal;
            string scriptBottomGlobal;

            string scriptBottomPage = null;

            //output the id of any page a/b test experiments



            var experiments = BaseCache.GetExperiments(AgilityContext.WebsiteName);
            var experiment  = experiments.GetForPage(currentPage.ID);

            if (experiment != null)
            {
                if (experiment.Variants != null &&
                    experiment.Variants.Any(v => !string.IsNullOrWhiteSpace(v.URL)))
                {
                    //PAGE REDIRECT EXPERIMENTS ONLY
                    AgilityContext.ExperimentKeys.Add(experiment.Key);


                    sb.AppendLine("<script type='text/javascript'>");

                    //get the winner
                    if (experiment.WinningVariant != null)
                    {
                        sb.AppendFormat("window.AgilityExperimentWinningVariant = {{ experiment: '{0}' variant: {1}, url: '{2}' }};",
                                        experiment.Key,
                                        experiment.WinningVariant.ID,
                                        AgilityHelpers.ResolveUrl(experiment.WinningVariant.URL)
                                        );
                    }
                    else
                    {
                        sb.Append("window.AgilityExperimentVariants = [");
                        foreach (var v in experiment.Variants)
                        {
                            sb.AppendFormat("{{ experiment: '{0}', variant: {1}, url: '{2}' }},",
                                            experiment.Key,
                                            v.ID,
                                            AgilityHelpers.ResolveUrl(v.URL)
                                            );
                        }

                        sb.AppendLine("];");
                        sb.AppendLine("</script>");
                    }
                }
            }
            else if (currentPage.ServerPage.ExperimentIDs != null)
            {
                foreach (int exId in currentPage.ServerPage.ExperimentIDs)
                {
                    experiment = experiments.GetExperiment(exId);
                    if (experiment != null)
                    {
                        AgilityContext.ExperimentKeys.Add(experiment.Key);
                    }
                }
            }


            //add the Javascript tracking stuff
            if (currentPage.IncludeInStatsTracking)
            {
                //global script
                if (!string.IsNullOrEmpty(AgilityContext.Domain.StatsTrackingScript))
                {
                    scriptTopGlobal = AgilityContext.Domain.StatsTrackingScript;

                    if (scriptTopGlobal.IndexOf(AgilityHelpers.GLOBAL_SCRIPT_SEPARATOR) != -1)
                    {
                        scriptBottomGlobal = scriptTopGlobal.Substring(scriptTopGlobal.IndexOf(AgilityHelpers.GLOBAL_SCRIPT_SEPARATOR) + AgilityHelpers.GLOBAL_SCRIPT_SEPARATOR.Length);
                        scriptTopGlobal    = scriptTopGlobal.Substring(0, scriptTopGlobal.IndexOf(AgilityHelpers.GLOBAL_SCRIPT_SEPARATOR));
                    }

                    if (!string.IsNullOrEmpty(scriptTopGlobal))
                    {
                        sb.Append(scriptTopGlobal);
                        sb.Append(Environment.NewLine);
                    }
                }
            }

            string scriptTopPage = null;

            //custom script for this page
            if (!string.IsNullOrEmpty(currentPage.CustomAnalyticsScript))
            {
                scriptTopPage = currentPage.CustomAnalyticsScript;

                if (scriptTopPage.IndexOf(AgilityHelpers.GLOBAL_SCRIPT_SEPARATOR) != -1)
                {
                    scriptBottomPage = scriptTopPage.Substring(scriptTopPage.IndexOf(AgilityHelpers.GLOBAL_SCRIPT_SEPARATOR) + AgilityHelpers.GLOBAL_SCRIPT_SEPARATOR.Length);
                    scriptTopPage    = scriptTopPage.Substring(0, scriptTopPage.IndexOf(AgilityHelpers.GLOBAL_SCRIPT_SEPARATOR));
                }

                if (!string.IsNullOrEmpty(scriptTopPage))
                {
                    sb.Append(scriptTopPage);
                    sb.Append(Environment.NewLine);
                }
            }

            return(new HtmlString(sb.ToString()));
        }
Example #2
0
        public async Task <HtmlString> InvokeAsync()
        {
            AgilityContext.HttpContext = HttpContext;

            AgilityPage currentPage = AgilityContext.Page;

            StringBuilder sb = new StringBuilder(Environment.NewLine);

            if (currentPage != null)
            {
                //canonical link
                if (!string.IsNullOrEmpty(AgilityContext.CanonicalLink))
                {
                    sb.AppendFormat("<link rel=\"canonical\" href=\"{0}\" />", AgilityContext.CanonicalLink);
                    sb.Append(Environment.NewLine);
                }

                //set the page specific meta tags
                sb.Append("<meta name=\"description\" content=\"").Append(currentPage.MetaTags).Append("\" />");
                sb.Append(Environment.NewLine);

                if (!string.IsNullOrEmpty(currentPage.MetaKeyWords))
                {
                    sb.Append("<meta name=\"keywords\" content=\"").Append(currentPage.MetaKeyWords).Append("\" />");
                    sb.Append(Environment.NewLine);
                }

                string rawTags = currentPage.MetaTagsRaw;
                if (rawTags == null)
                {
                    rawTags = string.Empty;
                }

                if (!string.IsNullOrEmpty(rawTags))
                {
                    sb.Append(Agility.Web.Util.Url.ResolveTildaUrlsInHtml(rawTags));
                    sb.Append(Environment.NewLine);
                }

                if (!string.IsNullOrEmpty(AgilityContext.TwitterCardSite))
                {
                    if (rawTags.IndexOf("<meta name=\"twitter:site\"", StringComparison.CurrentCultureIgnoreCase) == -1)
                    {
                        string site = AgilityContext.TwitterCardSite;
                        if (!site.StartsWith("@"))
                        {
                            site = string.Format("@{0}", site);
                        }

                        sb.AppendFormat("<meta name=\"twitter:site\" value=\"{0}\" />", site);
                    }

                    string twitterCardType = "summary";
                    if (!string.IsNullOrEmpty(AgilityContext.FeaturedImageUrl))
                    {
                        twitterCardType = "summary_large_image";
                        sb.AppendFormat("<meta name=\"twitter:image:src\" content=\"{0}\" />", AgilityContext.FeaturedImageUrl);
                    }

                    sb.AppendFormat("<meta name=\"twitter:card\" content=\"{0}\" />", twitterCardType);
                    sb.AppendFormat("<meta name=\"twitter:title\" content=\"{0}\" />", currentPage.Title);
                    sb.AppendFormat("<meta name=\"twitter:description\" content=\"{0}\" />", currentPage.MetaTags);
                }

                if (Current.Settings.OutputOpenGraph)
                {
                    sb.AppendFormat("<meta name=\"og:title\" content=\"{0}\" />", currentPage.Title);
                    sb.AppendFormat("<meta name=\"og:description\" content=\"{0}\" />", currentPage.MetaTags);
                    if (!string.IsNullOrEmpty(AgilityContext.FeaturedImageUrl))
                    {
                        sb.AppendFormat("<meta name=\"og:image\" content=\"{0}\" />", AgilityContext.FeaturedImageUrl);
                        sb.Append(Environment.NewLine);
                    }
                }

                //content language
                sb.Append("<meta http-equiv='content-language' content='").Append(currentPage.LanguageCode).Append("'/>");
                sb.Append(Environment.NewLine);

                //default css...
                string globalCss = AgilityContext.Domain.GlobalCss;
                if (!string.IsNullOrEmpty(globalCss))
                {
                    string url = AgilityHelpers.ResolveUrl(string.Format("~/{0}/global.css",
                                                                         Agility.Web.HttpModules.AgilityHttpModule.ECMS_EDITOR_CSS_KEY));
                    sb.AppendFormat("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", url);
                }


                //set the custom agility meta tags

                sb.Append("<meta name=\"generator\" content=\"Agility CMS\" />");
                sb.Append(Environment.NewLine);

                sb.AppendFormat("<meta name=\"agility_timestamp\" content=\"{0:yyyy/MM/dd hh:mm:ss tt}\" />", DateTime.Now);

                sb.AppendFormat("<meta name=\"agility_attributes\" content=\"Mode={0}, IsPreview={1}, Language={2}, Machine={3}, CustomOutputCache={4}\" />",
                                AgilityContext.CurrentMode,
                                AgilityContext.IsPreview,
                                AgilityContext.LanguageCode,
                                Environment.MachineName,
                                AgilityCache.UseAgilityOutputCache);
                sb.Append(Environment.NewLine);
            }

            //add the StatusPanelEmitter if in preview mode, development mode, or edit in place
            if (AgilityContext.IsPreview ||
                Current.Settings.DevelopmentMode ||
                AgilityContext.IsTemplatePreview)
            {
                sb.Append(StatusPanelEmitter.GetStatusPanelCssOnly());
            }


            return(new HtmlString(sb.ToString()));
        }