11# Copyright 2021 Google LLC
22#
3- # Licensed under the Apache License, Version 2.0 (the " License" );
3+ # Licensed under the Apache License, Version 2.0 (the ' License' );
44# you may not use this file except in compliance with the License.
55# You may obtain a copy of the License at
66#
77# https://www.apache.org/licenses/LICENSE-2.0
88#
99# Unless required by applicable law or agreed to in writing, software
10- # distributed under the License is distributed on an " AS IS" BASIS,
10+ # distributed under the License is distributed on an ' AS IS' BASIS,
1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
@@ -46,7 +46,7 @@ def test_exception_event_and_user_property(uploader, caplog):
4646 ])
4747 source = Source ('orig1' , SourceType .BIG_QUERY , [])
4848 execution = Execution (_account_config , source , destination )
49- with pytest .raises (ValueError , match = " GA4 MP should be called either for sending events" ):
49+ with pytest .raises (ValueError , match = ' GA4 MP should be called either for sending events' ):
5050 next (uploader .process (Batch (execution , [])))
5151
5252
@@ -64,7 +64,7 @@ def test_exception_no_event_nor_user_property(uploader, caplog):
6464 ])
6565 source = Source ('orig1' , SourceType .BIG_QUERY , [])
6666 execution = Execution (_account_config , source , destination )
67- with pytest .raises (ValueError , match = " GA4 MP should be called either for sending events" ):
67+ with pytest .raises (ValueError , match = ' GA4 MP should be called either for sending events' ):
6868 next (uploader .process (Batch (execution , [])))
6969
7070
@@ -82,7 +82,7 @@ def test_exception_app_and_web(uploader, caplog):
8282 ])
8383 source = Source ('orig1' , SourceType .BIG_QUERY , [])
8484 execution = Execution (_account_config , source , destination )
85- with pytest .raises (ValueError , match = " GA4 MP should be called either with a firebase_app_id" ):
85+ with pytest .raises (ValueError , match = ' GA4 MP should be called either with a firebase_app_id' ):
8686 next (uploader .process (Batch (execution , [{
8787 'name' : 'event_name' ,
8888 }])))
@@ -102,12 +102,55 @@ def test_exception_no_id(uploader, caplog):
102102 ])
103103 source = Source ('orig1' , SourceType .BIG_QUERY , [])
104104 execution = Execution (_account_config , source , destination )
105- with pytest .raises (ValueError , match = " GA4 MP should be called either with a firebase_app_id" ):
105+ with pytest .raises (ValueError , match = ' GA4 MP should be called either with a firebase_app_id' ):
106106 next (uploader .process (Batch (execution , [{
107107 'name' : 'event_name' ,
108108 'value' : '123'
109109 }])))
110110
111+ def test_exception_app_event_without_app_instance_id (uploader , caplog ):
112+ with requests_mock .Mocker () as m :
113+ m .post (requests_mock .ANY , status_code = 204 )
114+ destination = Destination (
115+ 'dest1' , DestinationType .GA_4_MEASUREMENT_PROTOCOL , [
116+ 'api_secret' ,
117+ 'True' ,
118+ 'False' ,
119+ '' ,
120+ 'some_id' ,
121+ ''
122+ ])
123+ source = Source ('orig1' , SourceType .BIG_QUERY , [])
124+ execution = Execution (_account_config , source , destination )
125+ with pytest .raises (ValueError , match = 'GA4 MP needs an app_instance_id parameter when used for an App Stream.' ):
126+ next (uploader .process (Batch (execution , [{
127+ 'client_id' : '123' ,
128+ 'name' : 'event_name' ,
129+ 'value' : '42' ,
130+ 'important_event' : 'False'
131+ }])))
132+
133+ def test_exception_web_event_without_client_id (uploader , caplog ):
134+ with requests_mock .Mocker () as m :
135+ m .post (requests_mock .ANY , status_code = 204 )
136+ destination = Destination (
137+ 'dest1' , DestinationType .GA_4_MEASUREMENT_PROTOCOL , [
138+ 'api_secret' ,
139+ 'True' ,
140+ 'False' ,
141+ '' ,
142+ '' ,
143+ 'some_id'
144+ ])
145+ source = Source ('orig1' , SourceType .BIG_QUERY , [])
146+ execution = Execution (_account_config , source , destination )
147+ with pytest .raises (ValueError , match = 'GA4 MP needs a client_id parameter when used for a Web Stream.' ):
148+ next (uploader .process (Batch (execution , [{
149+ 'app_instance_id' : '123' ,
150+ 'name' : 'event_name' ,
151+ 'value' : '42' ,
152+ 'important_event' : 'False'
153+ }])))
111154
112155def test_succesful_app_event_call (uploader , caplog ):
113156 with requests_mock .Mocker () as m :
@@ -124,14 +167,39 @@ def test_succesful_app_event_call(uploader, caplog):
124167 source = Source ('orig1' , SourceType .BIG_QUERY , [])
125168 execution = Execution (_account_config , source , destination )
126169 next (uploader .process (Batch (execution , [{
127- 'firebase_app_id ' : '123' ,
170+ 'app_instance_id ' : '123' ,
128171 'name' : 'event_name' ,
129172 'value' : '42' ,
130173 'important_event' : 'False'
131174 }])))
132175
133176 assert m .call_count == 1
134- assert m .last_request .json ()["events" ][0 ]["params" ]["value" ] == '42'
177+ assert m .last_request .json ()['events' ][0 ]['params' ]['value' ] == '42'
178+
179+
180+ def test_succesful_app_event_call_with_user_id (uploader , caplog ):
181+ with requests_mock .Mocker () as m :
182+ m .post (requests_mock .ANY , status_code = 204 )
183+ destination = Destination (
184+ 'dest1' , DestinationType .GA_4_MEASUREMENT_PROTOCOL , [
185+ 'api_secret' ,
186+ 'True' ,
187+ 'False' ,
188+ '' ,
189+ 'some_id' ,
190+ ''
191+ ])
192+ source = Source ('orig1' , SourceType .BIG_QUERY , [])
193+ execution = Execution (_account_config , source , destination )
194+ next (uploader .process (Batch (execution , [{
195+ 'app_instance_id' : '123' ,
196+ 'name' : 'event_name' ,
197+ 'value' : '42' ,
198+ 'user_id' : 'Id42'
199+ }])))
200+
201+ assert m .call_count == 1
202+ assert m .last_request .json ()['user_id' ] == 'Id42'
135203
136204
137205def test_succesful_web_user_property_call (uploader , caplog ):
@@ -149,13 +217,41 @@ def test_succesful_web_user_property_call(uploader, caplog):
149217 source = Source ('orig1' , SourceType .BIG_QUERY , [])
150218 execution = Execution (_account_config , source , destination )
151219 next (uploader .process (Batch (execution , [{
152- 'user_ltv' : '42'
220+ 'user_ltv' : '42' ,
221+ 'client_id' : 'some_id'
153222 },
154223 {
155- 'user_will_churn' : 'Maybe'
224+ 'user_will_churn' : 'Maybe' ,
225+ 'client_id' : 'some_id'
156226 }
157227 ])))
158228
159229 assert m .call_count == 2
160230 assert m .last_request .json (
161- )["userProperties" ]["user_will_churn" ]["value" ] == 'Maybe'
231+ )['userProperties' ]['user_will_churn' ]['value' ] == 'Maybe'
232+
233+ def test_succesful_web_user_property_call_with_user_id (uploader , caplog ):
234+ with requests_mock .Mocker () as m :
235+ m .post (requests_mock .ANY , status_code = 204 )
236+ destination = Destination (
237+ 'dest1' , DestinationType .GA_4_MEASUREMENT_PROTOCOL , [
238+ 'api_secret' ,
239+ 'False' ,
240+ 'True' ,
241+ '' ,
242+ '' ,
243+ 'some_id'
244+ ])
245+ source = Source ('orig1' , SourceType .BIG_QUERY , [])
246+ execution = Execution (_account_config , source , destination )
247+ next (uploader .process (Batch (execution , [{
248+ 'user_ltv' : '42' ,
249+ 'user_id' : 'Id42' ,
250+ 'client_id' : 'someId'
251+ }
252+ ])))
253+
254+ assert m .call_count == 1
255+ assert m .last_request .json (
256+ )['user_id' ] == 'Id42'
257+
0 commit comments