forked from scullyio/scully
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scully): Add GoogleAnalytics render plugin. (scullyio#705)
* feat(scully): Add GoogleAnalytics render plugin. * chore(lib): add config for analytics and revert logrocket prod-config * feat(scully): Add GoogleAnalytics render plugin. * chore(lib): add config for analytics and revert logrocket prod-config * fix(lib): add proper readme * fix(monorepo): fix typo * test(docsWeb): update snapshot Co-authored-by: jorgeucano <[email protected]> Co-authored-by: GuzmanPI <[email protected]> Co-authored-by: sanderelias <[email protected]>
- Loading branch information
Showing
30 changed files
with
598 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 4 additions & 21 deletions
25
libs/plugins/base-href-rewrite/src/lib/plugins-base-href-rewrite.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "extends": "../../../.eslintrc", "rules": {}, "ignorePatterns": ["!**/*"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# plugins-google-analytics | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
# Run `ng test plugins-google-analytics` to execute the unit tests via [Jest](https://jestjs.io). | ||
|
||
# Google Analytics | ||
|
||
## Description | ||
|
||
This plugin allows the usage of Google Analytics via Global Site Tag. | ||
|
||
## Type | ||
|
||
Render Plugin | ||
|
||
## Usage | ||
|
||
In the application's scully.<your-app>.config.ts: | ||
|
||
1. Configure the plugin: | ||
|
||
The plugin's configuration receives an object like this `{globalSiteTag: string}` where | ||
the `globalSiteTag` is the `gtag` provided by Google Analytics. | ||
|
||
2. Make a default post render array and add the plugin to it. | ||
|
||
3. Set the default post renders in Scully config. | ||
|
||
e.g. | ||
`./scully.<your-app>.config.ts` | ||
|
||
```typescript | ||
import { setPluginConfig, ScullyConfig, prod } from '@scullyio/scully'; | ||
import { GoogleAnalytics } from '@scullyio/plugins/google-analytics'; | ||
|
||
const defaultPostRenderers = []; | ||
|
||
if (prod) { | ||
setPluginConfig(GoogleAnalytics, { globalSiteTag: 'UA-#########-1' }); | ||
defaultPostRenderers.push(GoogleAnalytics); | ||
} | ||
export const config: ScullyConfig = { | ||
defaultPostRenderers, | ||
routes: { | ||
'/': { | ||
type: 'contentFolder', | ||
postRenderers: [...defaultPostRenderers], | ||
}, | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
name: 'plugins-google-analytics', | ||
preset: '../../../jest.config.js', | ||
transform: { | ||
'^.+\\.[tj]sx?$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], | ||
coverageDirectory: '../../../coverage/libs/plugins/google-analytics', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "@scullyio/plugins-google-analytics", | ||
"version": "0.0.1", | ||
"author": "Israel Guzman", | ||
"repository": { | ||
"type": "GIT", | ||
"url": "https://github.com/scullyio/scully/tree/main/libs/plugins/google-analytics" | ||
}, | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/plugins-google-analytics'; |
27 changes: 27 additions & 0 deletions
27
libs/plugins/google-analytics/src/lib/plugins-google-analytics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { registerPlugin, getMyConfig } from '@scullyio/scully'; | ||
|
||
export const GoogleAnalytics = 'googleAnalytics'; | ||
|
||
export const googleAnalyticsPlugin = async (html: string): Promise<string> => { | ||
const googleAnalyticsConfig = getMyConfig(googleAnalyticsPlugin); | ||
|
||
if (!googleAnalyticsConfig) { | ||
throw new Error('googleAnalytics plugin missing Global Site Tag'); | ||
} | ||
const siteTag: string = googleAnalyticsConfig['globalSiteTag']; | ||
|
||
const googleAnalyticsScript = ` | ||
<script async src="https://www.googletagmanager.com/gtag/js?id=${siteTag}"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${siteTag}'); | ||
</script>`; | ||
|
||
return html.replace(/<\/head/i, `${googleAnalyticsScript}</head`); | ||
}; | ||
|
||
const validator = async () => []; | ||
|
||
registerPlugin('render', GoogleAnalytics, googleAnalyticsPlugin, validator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node", "jest"] | ||
}, | ||
"include": ["**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"outDir": "../../../dist/out-tsc", | ||
"declaration": true, | ||
"rootDir": "./src", | ||
"types": ["node"] | ||
}, | ||
"exclude": ["**/*.spec.ts"], | ||
"include": ["**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"include": [ | ||
"**/*.spec.ts", | ||
"**/*.spec.tsx", | ||
"**/*.spec.js", | ||
"**/*.spec.jsx", | ||
"**/*.d.ts" | ||
] | ||
} |
Oops, something went wrong.