Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add script types and a simple type guard #27

Merged

Conversation

flashdesignory
Copy link
Collaborator

@flashdesignory flashdesignory commented Oct 5, 2023

just a starting point... could be exported differently, but at least it works.

with this we can do:

import { GoogleAnalytics, isExternalScript } from "third-party-capital";
import type { Script } from "third-party-capital";

const ga = GoogleAnalytics({ id: "donuts" });
ga.scripts.forEach((script:Script) => {
    console.log("external?", isExternalScript(script));
})

@kara

@@ -15,3 +15,5 @@
export { GoogleAnalytics } from './third-parties/google-analytics';
export { GoogleMapsEmbed } from './third-parties/google-maps-embed';
export { YouTubeEmbed } from './third-parties/youtube-embed';

export * from './types';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a way to get the types exported...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My TS skills are not as good as I'd like them to be lol

Is this how folks generally export types? Do you know if packages usually includes a types file as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's maybe a more "proper" way, this just made it work for me.
@janicklas-ralph - what did you have in mind?

would be nice maybe to be able to import them from "third-party-capital/types" instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah tbh I'm not sure, but we can figure that out in a future PR :)

@@ -11,7 +11,7 @@
"action": "append"
},
{
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','${args.id}')",
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','{{id}}')",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using a more traditional template tag

@@ -38,6 +39,12 @@ export function formatUrl(
return newUrl.toString();
}

export function formatCode(code: string, args?: Inputs) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function replaces the template tags with args passed in

@@ -11,7 +11,7 @@
"action": "append"
},
{
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','${args.id}')",
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(...arguments);};gtag({'gtm.start': Date.now(), 'event': 'gtm.js'});gtag({'config':'{{id}}'})",
Copy link
Collaborator Author

@flashdesignory flashdesignory Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part was a little weird, I don't know what the "config" does, but I kept it in there.

screenshot of ga tags applied to html (without this implementation):
Screenshot 2023-10-09 at 12 41 58 PM

screenshot of the modified code block (looks pretty close):
Screenshot 2023-10-09 at 12 42 09 PM

Copy link
Collaborator Author

@flashdesignory flashdesignory Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated screenshots - reverted to gtag usage:

snippet:
Screenshot 2023-10-10 at 11 29 24 AM
generated:
Screenshot 2023-10-10 at 11 29 53 AM

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird thing is that if we call the gtag function (either snippet or generated), it's basically an array, vs the automatic events fired are objects 🤷 .

I did compare with https://developers.google.com/tag-platform/gtagjs/configure and it looks right.

Copy link
Collaborator

@housseindjirdeh housseindjirdeh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one nit, otherwise this is great ❤️

Thanks for doing this!

@@ -11,8 +11,7 @@
"action": "append"
},
{
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','${args.id}')",
"strategy": "worker",
"code": "window.dataLayer=window.dataLayer||[];window.gtag=function gtag(){window.dataLayer.push(arguments);};gtag('js',new Date());gtag('config','{{id}}')", "strategy": "worker",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Identation

I thought I already set up Prettier in this package, but maybe I need to take another look 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ - fixed

@@ -15,3 +15,5 @@
export { GoogleAnalytics } from './third-parties/google-analytics';
export { GoogleMapsEmbed } from './third-parties/google-maps-embed';
export { YouTubeEmbed } from './third-parties/youtube-embed';

export * from './types';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah tbh I'm not sure, but we can figure that out in a future PR :)

@flashdesignory flashdesignory merged commit 04e7407 into GoogleChromeLabs:main Oct 11, 2023
2 checks passed
@flashdesignory flashdesignory deleted the thor-scripts-types branch October 11, 2023 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants