Skip to content

Commit

Permalink
event segregation (#356)
Browse files Browse the repository at this point in the history
* event segregation

* event segregation

* event segregation

* event segregation

* event segregation

* event segregation
  • Loading branch information
komal-sai-yral authored Aug 27, 2024
1 parent 2b444c6 commit 07cc6ab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
19 changes: 17 additions & 2 deletions ssr/src/utils/event_streaming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ pub struct EventHistory {

#[cfg(feature = "ga4")]
pub fn send_event(event_name: &str, params: &serde_json::Value) {
use super::posts::get_host;

let event_history: EventHistory = expect_context();

event_history.event_name.set(event_name.to_string());

let host_str = get_host();
let mut params = params.clone();
params["host"] = json!(host_str);

// Warehouse
send_event_warehouse(event_name, params);
send_event_warehouse(event_name, &params);

// gtag GA4
gtag("event", event_name, &JsValue::from_serde(params).unwrap());
gtag("event", event_name, &JsValue::from_serde(&params).unwrap());
}

#[cfg(feature = "ga4")]
Expand All @@ -55,7 +61,16 @@ pub fn send_user_id(user_id: String) {

#[cfg(feature = "ga4")]
pub fn send_event_warehouse(event_name: &str, params: &serde_json::Value) {
use super::posts::get_host;

let event_name = event_name.to_string();

let mut params = params.clone();
if params["host"].is_null() {
let host_str = get_host();
params["host"] = json!(host_str);
}

let params_str = params.to_string();

spawn_local(async move {
Expand Down
38 changes: 20 additions & 18 deletions ssr/src/utils/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,7 @@ pub async fn get_post_uid<const AUTH: bool>(

pub fn get_feed_component_identifier() -> impl Fn() -> Option<&'static str> {
move || {
let loc: String;

#[cfg(feature = "hydrate")]
{
use leptos::window;
loc = window().location().host().unwrap().to_string();
}

#[cfg(not(feature = "hydrate"))]
{
use axum::http::request::Parts;
use http::header::HeaderMap;
use leptos::expect_context;

let parts: Parts = expect_context();
let headers = parts.headers;
loc = headers.get("Host").unwrap().to_str().unwrap().to_string();
}
let loc = get_host();

if loc == "localhost:3000"
|| loc == "hotornot.wtf"
Expand All @@ -187,3 +170,22 @@ pub fn get_feed_component_identifier() -> impl Fn() -> Option<&'static str> {
}
}
}

pub fn get_host() -> String {
#[cfg(feature = "hydrate")]
{
use leptos::window;
window().location().host().unwrap().to_string()
}

#[cfg(not(feature = "hydrate"))]
{
use axum::http::request::Parts;
use http::header::HeaderMap;
use leptos::expect_context;

let parts: Parts = expect_context();
let headers = parts.headers;
headers.get("Host").unwrap().to_str().unwrap().to_string()
}
}

0 comments on commit 07cc6ab

Please sign in to comment.