Skip to content

Commit

Permalink
Fixes misuse of scripts registration
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelPetrinolis committed Jan 11, 2019
1 parent d814436 commit a72f06d
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public override async Task<IDisplayResult> EditAsync(GoogleAnalyticsSettings set
return Initialize<GoogleAnalyticsSettingsViewModel>("GoogleAnalyticsSettings_Edit", model =>
{
model.TrackingID = settings.TrackingID;
model.ScriptsAtHead = settings.ScriptsAtHead;
}).Location("Content:5").OnGroup(GoogleConstants.Features.GoogleAnalytics);
}

Expand All @@ -61,7 +60,6 @@ public override async Task<IDisplayResult> UpdateAsync(GoogleAnalyticsSettings s
if (context.Updater.ModelState.IsValid)
{
settings.TrackingID = model.TrackingID;
settings.ScriptsAtHead = model.ScriptsAtHead;
}
}
return await EditAsync(settings, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OrchardCore.Admin;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Layout;
using OrchardCore.Entities;
using OrchardCore.Google.Analytics.Settings;
using OrchardCore.ResourceManagement;
using OrchardCore.Settings;

namespace OrchardCore.Google.Analytics
{
public class GoogleAnalyticsFilter : IAsyncResultFilter
{
private readonly ILayoutAccessor _layoutAccessor;
private readonly IShapeFactory _shapeFactory;
private readonly IResourceManager _resourceManager;
private readonly ISiteService _siteService;

public GoogleAnalyticsFilter(
ILayoutAccessor layoutAccessor,
IShapeFactory shapeFactory)
IResourceManager resourceManager,
ISiteService siteService)
{
_layoutAccessor = layoutAccessor;
_shapeFactory = shapeFactory;
_resourceManager = resourceManager;
_siteService = siteService;
}

public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
Expand All @@ -27,10 +30,16 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE
if ((context.Result is ViewResult || context.Result is PageResult) &&
!AdminAttribute.IsApplied(context.HttpContext))
{
dynamic layout = await _layoutAccessor.GetLayoutAsync();
layout.Zones["Header"].Add(await _shapeFactory.CreateAsync("gtag"));
var settings = (await _siteService.GetSiteSettingsAsync()).As<GoogleAnalyticsSettings>();
if (!string.IsNullOrWhiteSpace(settings?.TrackingID))
{
_resourceManager.RegisterHeadScript(new HtmlString($"<script async src=\"https://www.googletagmanager.com/gtag/js?id={settings.TrackingID}\"></script>"));
_resourceManager.RegisterHeadScript(new HtmlString($"<script>window.dataLayer = window.dataLayer || [];function gtag() {{ dataLayer.push(arguments); }}gtag('js', new Date());gtag('config', '{settings.TrackingID}')</script>"));
}
}
await next.Invoke();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
container.Alter<GoogleAnalyticsSettings>(nameof(GoogleAnalyticsSettings), aspect =>
{
aspect.TrackingID = model.TrackingID;
aspect.ScriptsAtHead = model.ScriptsAtHead;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace OrchardCore.Google.Analytics.Settings
public class GoogleAnalyticsSettings
{
public string TrackingID { get; set; }
public bool ScriptsAtHead { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public class GoogleAnalyticsSettingsViewModel
{
[Required(AllowEmptyStrings = false)]
public string TrackingID { get; set; }
public bool ScriptsAtHead { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Views\gtag.cshtml">
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</EmbeddedResource>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,3 @@
<span class="hint">@T["The tracking ID defined in your Google Analytics account."]</span>
</div>
</div>

<div class="form-group row" asp-validation-class-for="ScriptsAtHead">
<div class="col-lg">
<div class="form-check">
<input asp-for="ScriptsAtHead" class="form-check-input" type="checkbox" checked="@Model.ScriptsAtHead" />
<label asp-for="ScriptsAtHead" class="form-check-label">@T["Put scripts at head"]</label>
</div>
<span asp-validation-for="ScriptsAtHead"></span>
<span class="hint">@T["Put javascript at head instead of foot."]</span>
</div>
</div>
17 changes: 0 additions & 17 deletions src/OrchardCore.Modules/OrchardCore.Google/Views/gtag.cshtml

This file was deleted.

0 comments on commit a72f06d

Please sign in to comment.