https://github.com/obsidiansystems/obelisk-google-analytics
https://github.com/obsidiansystems/obelisk-google-analytics
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/obsidiansystems/obelisk-google-analytics
- Owner: obsidiansystems
- License: bsd-3-clause
- Created: 2019-10-15T18:00:14.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-18T20:30:18.000Z (almost 3 years ago)
- Last Synced: 2024-12-24T21:35:15.587Z (5 months ago)
- Language: Haskell
- Size: 20.5 KB
- Stars: 2
- Watchers: 21
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Obelisk Google Analytics
Add google analytics tracking to your obelisk apps. Automatically sends `pageview` information when routes change. Also allows you to add more find-grained events to a reflex application.
This library is a thin binding to `gtag.js`, one of several offically supported client libraries for Google Analytics.
## Configuration
Go to https://analytics.google.com/analytics/web/, and set up an account if needed. From there, you can find a copy-and-pastable tracking ID you want to use for a given deployment, by clicking on the `Admin` settings in the lower left corner of the page, then selecting a `Property` at the top of the middle column, and then clicking `Property Settings` for your selected property. This tracking ID needs be put into a file located at:
```
config/frontend/google/analytics/tracking-id
```Note that if this file doesn't exist, analytics will be disabled, and no events will be sent to Google.
## Use in a Reflex Project
### Importing analytics
Simply include the `googleAnalytics` widget at the beginning of your `_frontend_head`: this will automatically capture the current route being viewed.
```
frontend :: Frontend (R Route)
frontend = Frontend
{ _frontend_head = googleAnalytics >> your_frontend_head
, _frontend_body = your_frontend_body
}
```### Adding additional events
In addition to the previous section, also put `mapRoutedT runGoogleAnalyticsT` around your `_frontend_body`. This will allow you to use `tellAnalytics` anywhere on the site.
```
frontend :: Frontend (R Route)
frontend = Frontend
{ _frontend_head = googleAnalytics >> your_frontend_head
, _frontend_body = mapRoutedT runGoogleAnalyticsT $ your_frontend_body
}
```We use `mapRoutedT` in order to put the `GoogleAnalyticsT` underneath the `RouteT`, and avoid having to generalize routing functions such as `subRoute_` in tricky ways. Here's an example of a widget that opens an external link in new window, and sends an analytics event when it happens:
```
extLinkAttr :: forall t m a. (DomBuilder t m, Analytics t m) => Map Text Text -> Text -> m a -> m a
extLinkAttr attrs href m = do
(e,a) <- elAttr' "a" ("href" =: href <> attrs <> "target" =: "_blank" <> "rel" =: "noopener") m
tellAnalytics (gaOutboundClickEvent href <$ (domEvent Click e :: Event t ()))
return a
```