gtm_js/lib.rs
1#![ deny(missing_docs)]
2
3//! This crate provides a rustic wrapper around Google's gtm.js tag manager snippet.
4//!
5//! To use this crate the tag manager must have already been imported as described in the relevant
6//! [documentation].
7//!
8//! [documentation]: https://developers.google.com/tag-manager/quickstart
9
10use wasm_bindgen::prelude::*;
11use serde::ser::Serialize;
12
13/// Push variables and/or events.
14///
15/// This function can fail if there is an error serializing the data to a [`JsValue`].
16///
17/// As a parameter value, this accepts any type that can be serialized to Json using Serde. This
18/// can be done by deriving `Serialize` from `serde` or using the `json!` macro from `serde_json`.
19///
20/// ```
21/// #[derive(Serialize)]
22/// struct PageviewEvent {
23/// event: String,
24/// page_path: String,
25/// }
26///
27/// impl PageviewEvent {
28/// fn new(path: String) -> Self {
29/// PageviewEvent {
30/// event: "custom-page-view",
31/// page_path: path,
32/// }
33/// }
34/// }
35/// ```
36/// ```
37/// json!({
38/// "event": "custom-page-view",
39/// "page_path": "/path",
40/// })
41/// ```
42///
43/// [`JsValue`]: https://docs.rs/wasm-bindgen/0.2.58/wasm_bindgen/struct.JsValue.html
44pub fn push(vars: &impl Serialize) -> serde_json::error::Result<()> {
45 gtm_js_sys::push(&JsValue::from_serde(vars)?);
46 Ok(())
47}