public override string BuildTrackingScript(ScriptBuilderContext appenderContext, SiteTrackerSettings siteSettings,
            out bool requiresScriptReference)
        {
            requiresScriptReference = false;
            if (siteSettings == null)
            {
                return null;
            }

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("/* Begin GA Script */");
            stringBuilder.AppendLine(_gaScript);

            stringBuilder.AppendLine(string.Format("ga('create', '{0}', 'auto');", siteSettings.TrackingId));

            stringBuilder.AppendLine("// Extended Tracking");
            stringBuilder.AppendLine(AppendExtendedTracking(siteSettings, ref requiresScriptReference));

            stringBuilder.AppendLine("// Plugin Script");
            stringBuilder.AppendLine(this.GetPluginScript());

            if (siteSettings.TrackAuthors && !string.IsNullOrEmpty(appenderContext.Author))
            {
                stringBuilder.AppendLine("// Custom Author Tracking");
                stringBuilder.AppendLine(this.GetCustomDimension("Author", CustomVariables.AuthorVariable, appenderContext.Author));
            }

            ContentReference contentReference = new ContentReference(appenderContext.PageId);

            ICollection<AnalyticsInteraction> interactions =  EPiServer.GoogleAnalytics.Helpers.Extensions.GetInteractions(appenderContext.InteractionStore);

            // This is where the interesting stuff happens
            // All custom interactions are added here
            stringBuilder.AppendLine("// Begin Interactions");
            foreach (AnalyticsInteraction interaction in interactions)
            {
                // Skip any interactions that are tied to a specific page
                if (ContentReference.IsNullOrEmpty(interaction.ContentLink) == false &&
                    contentReference.Equals(interaction.ContentLink) == false)
                {
                    continue;
                }
                stringBuilder.AppendLine(interaction.GetUAScript());
            }
            stringBuilder.AppendLine("// End Interactions");

            // Clear any interactions that should not persist
            // across a request
            this.ClearRedundantInteractions(interactions);

            stringBuilder.AppendLine("ga('send', 'pageview');");
            stringBuilder.AppendLine("/* End GA Script */");
            return stringBuilder.ToString();
        }
 public void Equals_IfTheObjectParameterIsTheSameInstance_ShouldReturnTrue()
 {
     ContentReference contentReference = new ContentReference();
     // ReSharper disable EqualExpressionComparison
     Assert.IsTrue(contentReference.Equals(contentReference));
     // ReSharper restore EqualExpressionComparison
 }
        public void Equals_IfTheParameterIsAPageReferenceAndThePropertiesAreEqual_ShouldReturnTrue()
        {
            PageReference pageReference = new PageReference(DateTime.Now.Millisecond, DateTime.Now.Second, DateTime.Now.Second%2 == 0 ? "Test" : null);
            ContentReference contentReference = new ContentReference(pageReference.ID, pageReference.WorkID, pageReference.RemoteSite);

            // ReSharper disable SuspiciousTypeConversion.Global
            Assert.IsTrue(contentReference.Equals(pageReference));
            // ReSharper restore SuspiciousTypeConversion.Global
        }