22
33use AlexWestergaard \PhpGa4 \Analytics ;
44use AlexWestergaard \PhpGa4 \Item ;
5- use AlexWestergaard \PhpGa4 \Model ;
6- use AlexWestergaard \PhpGa4 \Event ;
75use AlexWestergaard \PhpGa4 \UserProperty ;
86
97class AnalyticsTest extends \PHPUnit \Framework \TestCase
@@ -25,6 +23,7 @@ protected function prepareSituation()
2523 'user_id ' => 'm6435 ' ,
2624 // Default Vars
2725 'currency ' => 'EUR ' ,
26+ 'currency_virtual ' => 'GA4Coins ' ,
2827 ];
2928
3029 $ this ->analytics = Analytics::new ($ this ->prefill ['measurement_id ' ], $ this ->prefill ['api_secret ' ], /* DEBUG */ true )
@@ -35,7 +34,7 @@ protected function prepareSituation()
3534 ->setItemId ('1 ' )
3635 ->setItemName ('First Product ' )
3736 ->setCurrency ($ this ->prefill ['currency ' ])
38- ->setPrice (10 )
37+ ->setPrice (7.39 )
3938 ->setQuantity (2 );
4039 }
4140
@@ -91,4 +90,115 @@ public function testUserProperty()
9190
9291 $ this ->assertTrue ($ this ->analytics ->post ());
9392 }
93+
94+ public function testPrebuildEvents ()
95+ {
96+ $ this ->prepareSituation ();
97+
98+ $ eventCount = 0 ;
99+ foreach (glob (__DIR__ . '/../src/Event/*.php ' ) as $ file ) {
100+ $ eventName = 'AlexWestergaard \\PhpGa4 \\Event \\' . basename ($ file , '.php ' );
101+
102+ $ this ->assertTrue (class_exists ($ eventName ), $ eventName );
103+
104+ $ event = new $ eventName ;
105+ $ required = $ event ->getRequiredParams ();
106+ $ params = array_unique (array_merge ($ event ->getParams (), $ required ));
107+
108+ try {
109+ if (in_array ('currency ' , $ params )) {
110+ $ event ->setCurrency ($ this ->prefill ['currency ' ]);
111+ if (in_array ('value ' , $ params )) {
112+ $ event ->setValue (9.99 );
113+ }
114+ }
115+
116+ if (in_array ('price ' , $ params )) {
117+ $ event ->setPrice (9.99 );
118+ }
119+
120+ if (in_array ('quantity ' , $ params )) {
121+ $ event ->setQuantity (9.99 );
122+ }
123+
124+ if (in_array ('payment_type ' , $ params )) {
125+ $ event ->setPaymentType ('credit card ' );
126+ }
127+
128+ if (in_array ('shipping_tier ' , $ params )) {
129+ $ event ->setShippingTier ('ground ' );
130+ }
131+
132+ if (in_array ('items ' , $ params )) {
133+ if (method_exists ($ event , 'addItem ' )) {
134+ $ event ->addItem ($ this ->item );
135+ } elseif (method_exists ($ event , 'setItem ' )) {
136+ $ event ->setItem ($ this ->item );
137+ }
138+ }
139+
140+ if (in_array ('virtual_currency_name ' , $ params )) {
141+ $ event ->setVirtualCurrencyName ($ this ->prefill ['currency_virtual ' ]);
142+
143+ if (in_array ('value ' , $ params )) {
144+ $ event ->setValue (9.99 );
145+ }
146+
147+ if (in_array ('item_name ' , $ params )) {
148+ $ event ->setItemName ('CookieBite ' );
149+ }
150+ }
151+
152+ if (in_array ('character ' , $ params )) {
153+ $ event ->setCharacter ('AlexWestergaard ' );
154+
155+ if (in_array ('level ' , $ params )) {
156+ $ event ->setLEvel (3 );
157+ }
158+
159+ if (in_array ('score ' , $ params )) {
160+ $ event ->setScore (500 );
161+ }
162+ }
163+
164+ if (in_array ('location_id ' , $ params )) {
165+ $ event ->setLocationId ('ChIJeRpOeF67j4AR9ydy_PIzPuM ' );
166+ }
167+
168+ if (in_array ('transaction_id ' , $ params )) {
169+ $ event ->setTransactionId ('O6435DK ' );
170+ }
171+
172+ if (in_array ('achievement_id ' , $ params )) {
173+ $ event ->setAchievementId ('achievement_buy_5_items ' );
174+ }
175+
176+ $ this ->assertTrue (is_array ($ event ->toArray ()), $ eventName );
177+
178+ $ this ->analytics ->addEvent ($ event );
179+ $ eventCount += 1 ;
180+ } catch (throwable $ t ) {
181+ $ this ->assertTrue (false , $ t ->getFile () . ': ' . $ t ->getLine () . ' > ' . $ t ->getMessage ());
182+ }
183+
184+ if ($ eventCount >= 25 ) {
185+ try {
186+ $ this ->assertTrue ($ this ->analytics ->post ());
187+ } catch (throwable $ t ) {
188+ $ this ->assertTrue (false , $ t ->getFile () . ': ' . $ t ->getLine () . ' > ' . $ t ->getMessage ());
189+ } finally {
190+ $ eventCount = 1 ;
191+ $ this ->prepareSituation ();
192+ }
193+ }
194+ }
195+
196+ if ($ eventCount > 0 ) {
197+ try {
198+ $ this ->assertTrue ($ this ->analytics ->post ());
199+ } catch (throwable $ t ) {
200+ $ this ->assertTrue (false , $ t ->getFile () . ': ' . $ t ->getLine () . ' > ' . $ t ->getMessage ());
201+ }
202+ }
203+ }
94204}
0 commit comments