Skip to content

Commit 18466a6

Browse files
committed
Improve distribution of compaign order.
1 parent 6a7a34f commit 18466a6

File tree

1 file changed

+171
-44
lines changed

1 file changed

+171
-44
lines changed

includes/Generator/OrderAttribution.php

Lines changed: 171 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
class OrderAttribution {
1414

15+
/**
16+
* Campaign distribution percentages
17+
*/
18+
const CAMPAIGN_PROBABILITY = 15; // 15% of orders will have campaign data
19+
1520
/**
1621
* Generate order attribution data.
1722
*
@@ -30,18 +35,18 @@ public static function add_order_attribution_meta( $order, $assoc_args = array()
3035
return;
3136
}
3237

33-
$device_type = self::get_random_device_type();
34-
$source = 'woo.com';
35-
$source_type = self::get_source_type();
36-
$origin = self::get_origin( $source_type, $source );
37-
$product_url = get_permalink( $order_products[ array_rand( $order_products ) ]->get_id() );
38-
$utm_content = [ '/', 'campaign_a', 'campaign_b' ];
39-
$utm_content = $utm_content[ array_rand( $utm_content ) ];
38+
$device_type = self::get_random_device_type();
39+
$source = 'woo.com';
40+
$source_type = self::get_source_type();
41+
$origin = self::get_origin( $source_type, $source );
42+
$product_url = get_permalink( $order_products[ array_rand( $order_products ) ]->get_id() );
43+
$utm_content = array( '/', 'campaign_a', 'campaign_b' );
44+
$utm_content = $utm_content[ array_rand( $utm_content ) ];
4045

4146
$meta = array();
4247

4348
// For these source types, we only need to set the source type.
44-
if ( in_array( $source_type, [ 'admin', 'mobile_app', 'unknown' ], true ) ) {
49+
if ( in_array( $source_type, array( 'admin', 'mobile_app', 'unknown' ), true ) ) {
4550
$meta = array(
4651
'_wc_order_attribution_source_type' => $source_type,
4752
);
@@ -54,17 +59,20 @@ public static function add_order_attribution_meta( $order, $assoc_args = array()
5459
'_wc_order_attribution_session_pages' => wp_rand( 1, 10 ),
5560
'_wc_order_attribution_session_start_time' => self::get_random_session_start_time( $order ),
5661
'_wc_order_attribution_session_entry' => $product_url,
57-
'_wc_order_attribution_utm_content' => $utm_content,
58-
'_wc_order_attribution_utm_source' => self::get_source( $source_type ),
59-
'_wc_order_attribution_referrer' => self::get_referrer( $source_type ),
6062
'_wc_order_attribution_source_type' => $source_type,
61-
'_wc_order_attribution_utm_campaign' => self::get_random_utm_campaign(),
62-
'_wc_order_attribution_utm_term' => self::get_random_utm_term(),
6363
);
64+
65+
// Add campaign data only for a percentage of orders.
66+
if ( wp_rand( 1, 100 ) <= self::CAMPAIGN_PROBABILITY ) {
67+
$campaign_data = self::get_campaign_data();
68+
$meta = array_merge( $meta, $campaign_data );
69+
} else {
70+
$meta['_wc_order_attribution_utm_content'] = $utm_content;
71+
}
6472
}
6573

6674
// If the source type is not typein ( Direct ), set a random utm medium.
67-
if ( ! in_array( $source_type, [ 'typein', 'admin', 'mobile_app', 'unknown' ], true ) ) {
75+
if ( ! in_array( $source_type, array( 'typein', 'admin', 'mobile_app', 'unknown' ), true ) ) {
6876
$meta['_wc_order_attribution_utm_medium'] = self::get_random_utm_medium();
6977
}
7078

@@ -315,7 +323,7 @@ public static function get_random_session_start_time( $order ) {
315323
$order_created_date = clone $order->get_date_created();
316324

317325
// Random DateTimeInterval between 10 minutes and 6 hours.
318-
$random_interval = new \DateInterval( 'PT' . (string) wp_rand( 10, 360 ) . 'M' );
326+
$random_interval = new \DateInterval( 'PT' . (string) wp_rand( 10, 360 ) . 'M' );
319327

320328
// Subtract the random interval from the order creation date.
321329
$order_created_date->sub( $random_interval );
@@ -324,47 +332,166 @@ public static function get_random_session_start_time( $order ) {
324332
}
325333

326334
/**
327-
* Get a random UTM campaign name.
335+
* Get campaign attribution data.
336+
*
337+
* @return array Campaign attribution data.
338+
*/
339+
private static function get_campaign_data() {
340+
$campaign_type = self::get_campaign_type();
341+
342+
switch ( $campaign_type ) {
343+
case 'seasonal':
344+
return self::get_seasonal_campaign_data();
345+
case 'promotional':
346+
return self::get_promotional_campaign_data();
347+
case 'product':
348+
return self::get_product_campaign_data();
349+
default:
350+
return self::get_general_campaign_data();
351+
}
352+
}
353+
354+
/**
355+
* Get the campaign type based on weighted probabilities.
328356
*
329-
* @return string The UTM campaign name.
357+
* @return string Campaign type.
330358
*/
331-
public static function get_random_utm_campaign() {
359+
private static function get_campaign_type() {
360+
$random = wp_rand( 1, 100 );
361+
362+
if ( $random <= 40 ) {
363+
return 'seasonal'; // 40% seasonal campaigns
364+
} elseif ( $random <= 70 ) {
365+
return 'promotional'; // 30% promotional campaigns
366+
} elseif ( $random <= 90 ) {
367+
return 'product'; // 20% product campaigns
368+
} else {
369+
return 'general'; // 10% general campaigns
370+
}
371+
}
372+
373+
/**
374+
* Get seasonal campaign data.
375+
*
376+
* @return array Campaign data.
377+
*/
378+
private static function get_seasonal_campaign_data() {
332379
$campaigns = array(
333-
'summer_sale_2024',
334-
'black_friday_2024',
335-
'new_product_launch',
336-
'holiday_special',
337-
'spring_collection',
338-
'flash_sale',
339-
'membership_promo',
340-
'newsletter_special',
341-
'social_campaign',
342-
'influencer_collab',
380+
'summer_sale_2024' => array(
381+
'content' => 'summer_deals',
382+
'term' => 'seasonal_discount',
383+
),
384+
'black_friday_2024' => array(
385+
'content' => 'bf_deals',
386+
'term' => 'black_friday_sale',
387+
),
388+
'holiday_special' => array(
389+
'content' => 'holiday_deals',
390+
'term' => 'christmas_sale',
391+
),
343392
);
344393

345-
return $campaigns[ array_rand( $campaigns ) ];
394+
$campaign_name = array_rand( $campaigns );
395+
$campaign = $campaigns[ $campaign_name ];
396+
397+
return array(
398+
'_wc_order_attribution_utm_campaign' => $campaign_name,
399+
'_wc_order_attribution_utm_content' => $campaign['content'],
400+
'_wc_order_attribution_utm_term' => $campaign['term'],
401+
'_wc_order_attribution_utm_source' => 'email',
402+
'_wc_order_attribution_utm_medium' => 'email',
403+
);
346404
}
347405

348406
/**
349-
* Get a random UTM term (usually for paid search keywords).
407+
* Get promotional campaign data.
350408
*
351-
* @return string The UTM term.
409+
* @return array Campaign data.
352410
*/
353-
public static function get_random_utm_term() {
354-
$terms = array(
355-
'buy_online',
356-
'best_deals',
357-
'discount_code',
358-
'free_shipping',
359-
'premium_products',
360-
'sale_items',
361-
'new_arrival',
362-
'trending_products',
363-
'limited_offer',
364-
'', // Sometimes term might be empty
411+
private static function get_promotional_campaign_data() {
412+
$campaigns = array(
413+
'flash_sale' => array(
414+
'content' => '24hr_sale',
415+
'term' => 'limited_time',
416+
),
417+
'membership_promo' => array(
418+
'content' => 'member_exclusive',
419+
'term' => 'join_now',
420+
),
365421
);
366422

367-
return $terms[ array_rand( $terms ) ];
423+
$campaign_name = array_rand( $campaigns );
424+
$campaign = $campaigns[ $campaign_name ];
425+
426+
return array(
427+
'_wc_order_attribution_utm_campaign' => $campaign_name,
428+
'_wc_order_attribution_utm_content' => $campaign['content'],
429+
'_wc_order_attribution_utm_term' => $campaign['term'],
430+
'_wc_order_attribution_utm_source' => 'social',
431+
'_wc_order_attribution_utm_medium' => 'cpc',
432+
);
433+
}
434+
435+
/**
436+
* Get product campaign data.
437+
*
438+
* @return array Campaign data.
439+
*/
440+
private static function get_product_campaign_data() {
441+
$campaigns = array(
442+
'new_product_launch' => array(
443+
'content' => 'product_launch',
444+
'term' => 'new_arrival',
445+
),
446+
'spring_collection' => array(
447+
'content' => 'spring_2024',
448+
'term' => 'new_collection',
449+
),
450+
);
451+
452+
$campaign_name = array_rand( $campaigns );
453+
$campaign = $campaigns[ $campaign_name ];
454+
455+
return array(
456+
'_wc_order_attribution_utm_campaign' => $campaign_name,
457+
'_wc_order_attribution_utm_content' => $campaign['content'],
458+
'_wc_order_attribution_utm_term' => $campaign['term'],
459+
'_wc_order_attribution_utm_source' => 'google',
460+
'_wc_order_attribution_utm_medium' => 'cpc',
461+
);
462+
}
463+
464+
/**
465+
* Get general campaign data.
466+
*
467+
* @return array Campaign data.
468+
*/
469+
private static function get_general_campaign_data() {
470+
$campaigns = array(
471+
'newsletter_special' => array(
472+
'content' => 'newsletter_special',
473+
'term' => 'newsletter_special',
474+
),
475+
'social_campaign' => array(
476+
'content' => 'social_campaign',
477+
'term' => 'social_campaign',
478+
),
479+
'influencer_collab' => array(
480+
'content' => 'influencer_collab',
481+
'term' => 'influencer_collab',
482+
),
483+
);
484+
485+
$campaign_name = array_rand( $campaigns );
486+
$campaign = $campaigns[ $campaign_name ];
487+
488+
return array(
489+
'_wc_order_attribution_utm_campaign' => $campaign_name,
490+
'_wc_order_attribution_utm_content' => $campaign['content'],
491+
'_wc_order_attribution_utm_term' => $campaign['term'],
492+
'_wc_order_attribution_utm_source' => 'email',
493+
'_wc_order_attribution_utm_medium' => 'email',
494+
);
368495
}
369496

370497
}

0 commit comments

Comments
 (0)