Decorate Google tracking script generators

Hi, I want to decorate the Google tracking script generators, specifically TrackingScriptGeneratorGoogleTagManager and TrackingScriptGeneratorGoogleUniversal.

I try to decorate it by implementing the class, and adding the ServiceDecorator attribute. However, as the service is actually of type TrackingScriptGenerator I can only decorate that service, I guess. I’m not sure how I can decorate a specific implementation of a service, if you understand what I mean?

Example:

[ServiceDecorator(typeof(TrackingScriptGenerator), Name = nameof(TrackingScriptGeneratorGoogleTagManager))]
public class TrackingScriptGeneratorGoogleTagManagerImpl : TrackingScriptGeneratorGoogleTagManager
{
    private readonly TrackingScriptGeneratorGoogleTagManager _parent;

    public TrackingScriptGeneratorGoogleTagManagerImpl(TrackingScriptGeneratorGoogleTagManager parent)
    {
        _parent = parent;
    }
}

Using this code, I get the following exception:
A suitable constructor for type 'Litium.Accelerator.Mvc.Tracking.TrackingScriptGeneratorGoogleTagManagerImpl' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

Litium version: 7.7.9

Solved it by unregistering the default services first:

public class SomeAppConfig : IApplicationConfiguration
{
    public void Configure(ApplicationConfigurationBuilder app)
    {
        app.ConfigureServices(services =>
        {
            var trackingScriptGeneratorGoogleTagManager = services.FirstOrDefault(s => s.ImplementationType == typeof(TrackingScriptGeneratorGoogleTagManager));
            if (trackingScriptGeneratorGoogleTagManager != null)
            {
                services.Remove(trackingScriptGeneratorGoogleTagManager);
            }

            var trackingScriptGeneratorGoogleUniversal = services.FirstOrDefault(s => s.ImplementationType == typeof(TrackingScriptGeneratorGoogleUniversal));
            if (trackingScriptGeneratorGoogleUniversal != null)
            {
                services.Remove(trackingScriptGeneratorGoogleUniversal);
            }
        });
    }
}

I then implemented my own tracking script generators.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.