stormid / components

UI components
https://stormid.github.io/components/
MIT License
2 stars 2 forks source link

Cookie banner > add commonly used utilities to the banner API #229

Closed mjbp closed 2 years ago

mjbp commented 2 years ago

Both iframe embedding (based on replacing a div with data attributes), and Google Tag Manager snippets are used in every implementation of the cookie banner in our projects.

We should consider adding the snippets to the banner module?

mjbp commented 2 years ago

@sarah-storm @Nick-StormID @CatVine Before I take this any further, what do you think about this implementation? Is it any better than just having the iframe rendering and GA snippets in each project?

Adding these utils functions to the state Object that is passed to each of the functions executed based on cookie preferences

import cookieBanner from '@stormid/cookie-banner';

cookieBanner({
    ...lots of other config
    type: {
        ads: [
            state => state.utils.renderIframe(),
            state => state.utils.gaSnippet('UA-12345-6789')
        ]
    }
});

Note that due to the bundler config required to support older projects that need commonjs or umd we cannot have a default export and a named export. So this is not possible:

In the cookie banner module

export const renderIframe = () => ...

In a project index.js

import cookieBanner, { renderIframe, gaSnippet } from '@stormid/cookie-banner';

cookieBanner({
    ...lots of other config
    type: {
        ads: [
            renderIframe
            () => gaSnippet ('UA-12345-6789')
        ]
    }
});

hence the slightly more unusual implementation above.

sarah-storm commented 2 years ago

Hello! Yes, I think it's still worthwhile standardising the functions... still a reasonably manual process to enable them, but at least there's much less code being copy-pasted around in case of an update. Looks good to me!