@@ -19,8 +19,12 @@ class Analytics extends Helper\IOHelper implements Facade\Type\AnalyticsType
1919
2020 protected null |bool $ non_personalized_ads = false ;
2121 protected null |int $ timestamp_micros ;
22+ protected null |string $ validation_behavior ;
23+ protected null |string $ ip_override ;
2224 protected null |string $ client_id ;
2325 protected null |string $ user_id ;
26+ protected array $ user_location = [];
27+ protected array $ device = [];
2428 protected array $ user_properties = [];
2529 protected array $ events = [];
2630
@@ -40,8 +44,12 @@ public function getParams(): array
4044 return [
4145 'non_personalized_ads ' ,
4246 'timestamp_micros ' ,
47+ 'validation_behavior ' ,
48+ 'ip_override ' ,
4349 'client_id ' ,
4450 'user_id ' ,
51+ 'user_location ' ,
52+ 'device ' ,
4553 'user_properties ' ,
4654 'events ' ,
4755 ];
@@ -68,6 +76,12 @@ public function setClientId(string $id)
6876 return $ this ;
6977 }
7078
79+ public function setNonPersonalizedAds (bool $ enabled = true )
80+ {
81+ $ this ->non_personalized_ads = $ enabled ;
82+ return $ this ;
83+ }
84+
7185 public function setUserId (string $ id )
7286 {
7387 $ this ->user_id = $ id ;
@@ -89,6 +103,30 @@ public function setTimestampMicros(int|float $microOrUnix)
89103 return $ this ;
90104 }
91105
106+ public function setValidationBehavior (null |string $ behavior )
107+ {
108+ $ this ->validation_behavior = $ behavior ;
109+ return $ this ;
110+ }
111+
112+ public function setIpOverride (null |string $ ip )
113+ {
114+ $ this ->ip_override = $ ip ;
115+ return $ this ;
116+ }
117+
118+ public function setUserLocation (array $ location )
119+ {
120+ $ this ->user_location = static ::cleanAssoc ($ location );
121+ return $ this ;
122+ }
123+
124+ public function setDevice (array $ device )
125+ {
126+ $ this ->device = static ::cleanAssoc ($ device );
127+ return $ this ;
128+ }
129+
92130 public function addUserProperty (Facade \Type \UserPropertyType ...$ props )
93131 {
94132 foreach ($ props as $ prop ) {
@@ -130,9 +168,11 @@ public function post(): void
130168 $ url = $ this ->debug ? Facade \Type \AnalyticsType::URL_DEBUG : Facade \Type \AnalyticsType::URL_LIVE ;
131169 $ url .= '? ' . http_build_query (['measurement_id ' => $ this ->measurement_id , 'api_secret ' => $ this ->api_secret ]);
132170
171+ $ userData = $ this ->userdata ->toArray ();
172+
133173 $ body = array_replace_recursive (
134174 $ this ->toArray (),
135- ["user_data " => ! empty ( $ this -> user_id ) ? $ this -> userdata -> toArray () : []], // Only accepted if user_id is passed too
175+ ["user_data " => $ userData ],
136176 ["user_properties " => $ this ->user_properties ],
137177 ["consent " => $ this ->consent ->toArray ()],
138178 );
@@ -197,4 +237,22 @@ public static function new(string $measurement_id, string $api_secret, bool $deb
197237 {
198238 return new static ($ measurement_id , $ api_secret , $ debug );
199239 }
240+
241+ private static function cleanAssoc (array $ input ): array
242+ {
243+ $ out = [];
244+ foreach ($ input as $ key => $ value ) {
245+ if (!is_string ($ key )) {
246+ continue ;
247+ }
248+
249+ if ($ value === null || $ value === '' ) {
250+ continue ;
251+ }
252+
253+ $ out [$ key ] = $ value ;
254+ }
255+
256+ return $ out ;
257+ }
200258}
0 commit comments