Skip to content

Commit ce9e396

Browse files
authored
Merge pull request #146 from woocommerce/add/add-support-for-utm-order-data
Add support for campaign order attribution data.
2 parents 6778005 + 7b50c44 commit ce9e396

File tree

1 file changed

+176
-10
lines changed

1 file changed

+176
-10
lines changed

includes/Generator/OrderAttribution.php

Lines changed: 176 additions & 10 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
);
@@ -59,10 +64,16 @@ public static function add_order_attribution_meta( $order, $assoc_args = array()
5964
'_wc_order_attribution_referrer' => self::get_referrer( $source_type ),
6065
'_wc_order_attribution_source_type' => $source_type,
6166
);
67+
68+
// Add campaign data only for a percentage of orders.
69+
if ( wp_rand( 1, 100 ) <= self::CAMPAIGN_PROBABILITY ) {
70+
$campaign_data = self::get_campaign_data();
71+
$meta = array_merge( $meta, $campaign_data );
72+
}
6273
}
6374

6475
// If the source type is not typein ( Direct ), set a random utm medium.
65-
if ( ! in_array( $source_type, [ 'typein', 'admin', 'mobile_app', 'unknown' ], true ) ) {
76+
if ( ! in_array( $source_type, array( 'typein', 'admin', 'mobile_app', 'unknown' ), true ) ) {
6677
$meta['_wc_order_attribution_utm_medium'] = self::get_random_utm_medium();
6778
}
6879

@@ -313,12 +324,167 @@ public static function get_random_session_start_time( $order ) {
313324
$order_created_date = clone $order->get_date_created();
314325

315326
// Random DateTimeInterval between 10 minutes and 6 hours.
316-
$random_interval = new \DateInterval( 'PT' . (string) wp_rand( 10, 360 ) . 'M' );
327+
$random_interval = new \DateInterval( 'PT' . (string) wp_rand( 10, 360 ) . 'M' );
317328

318329
// Subtract the random interval from the order creation date.
319330
$order_created_date->sub( $random_interval );
320331

321332
return $order_created_date->format( 'Y-m-d H:i:s' );
322333
}
323334

335+
/**
336+
* Get campaign attribution data.
337+
*
338+
* @return array Campaign attribution data.
339+
*/
340+
private static function get_campaign_data() {
341+
$campaign_type = self::get_campaign_type();
342+
343+
switch ( $campaign_type ) {
344+
case 'seasonal':
345+
return self::get_seasonal_campaign_data();
346+
case 'promotional':
347+
return self::get_promotional_campaign_data();
348+
case 'product':
349+
return self::get_product_campaign_data();
350+
default:
351+
return self::get_general_campaign_data();
352+
}
353+
}
354+
355+
/**
356+
* Get the campaign type based on weighted probabilities.
357+
*
358+
* @return string Campaign type.
359+
*/
360+
private static function get_campaign_type() {
361+
$random = wp_rand( 1, 100 );
362+
363+
if ( $random <= 40 ) {
364+
return 'seasonal'; // 40% seasonal campaigns
365+
} elseif ( $random <= 70 ) {
366+
return 'promotional'; // 30% promotional campaigns
367+
} elseif ( $random <= 90 ) {
368+
return 'product'; // 20% product campaigns
369+
} else {
370+
return 'general'; // 10% general campaigns
371+
}
372+
}
373+
374+
/**
375+
* Get seasonal campaign data.
376+
*
377+
* @return array Campaign data.
378+
*/
379+
private static function get_seasonal_campaign_data() {
380+
$campaigns = array(
381+
'summer_sale' => array(
382+
'content' => 'summer_deals',
383+
'term' => 'seasonal_discount',
384+
),
385+
'black_friday' => array(
386+
'content' => 'bf_deals',
387+
'term' => 'black_friday_sale',
388+
),
389+
'holiday_special' => array(
390+
'content' => 'holiday_deals',
391+
'term' => 'christmas_sale',
392+
),
393+
);
394+
395+
$campaign_name = array_rand( $campaigns );
396+
$campaign = $campaigns[ $campaign_name ];
397+
398+
return array(
399+
'_wc_order_attribution_utm_campaign' => $campaign_name,
400+
'_wc_order_attribution_utm_content' => $campaign['content'],
401+
'_wc_order_attribution_utm_term' => $campaign['term'],
402+
);
403+
}
404+
405+
/**
406+
* Get promotional campaign data.
407+
*
408+
* @return array Campaign data.
409+
*/
410+
private static function get_promotional_campaign_data() {
411+
$campaigns = array(
412+
'flash_sale' => array(
413+
'content' => '24hr_sale',
414+
'term' => 'limited_time',
415+
),
416+
'membership_promo' => array(
417+
'content' => 'member_exclusive',
418+
'term' => 'join_now',
419+
),
420+
);
421+
422+
$campaign_name = array_rand( $campaigns );
423+
$campaign = $campaigns[ $campaign_name ];
424+
425+
return array(
426+
'_wc_order_attribution_utm_campaign' => $campaign_name,
427+
'_wc_order_attribution_utm_content' => $campaign['content'],
428+
'_wc_order_attribution_utm_term' => $campaign['term'],
429+
);
430+
}
431+
432+
/**
433+
* Get product campaign data.
434+
*
435+
* @return array Campaign data.
436+
*/
437+
private static function get_product_campaign_data() {
438+
$campaigns = array(
439+
'new_product_launch' => array(
440+
'content' => 'product_launch',
441+
'term' => 'new_arrival',
442+
),
443+
'spring_collection' => array(
444+
'content' => 'spring',
445+
'term' => 'new_collection',
446+
),
447+
);
448+
449+
$campaign_name = array_rand( $campaigns );
450+
$campaign = $campaigns[ $campaign_name ];
451+
452+
return array(
453+
'_wc_order_attribution_utm_campaign' => $campaign_name,
454+
'_wc_order_attribution_utm_content' => $campaign['content'],
455+
'_wc_order_attribution_utm_term' => $campaign['term'],
456+
);
457+
}
458+
459+
/**
460+
* Get general campaign data.
461+
*
462+
* @return array Campaign data.
463+
*/
464+
private static function get_general_campaign_data() {
465+
$campaigns = array(
466+
'newsletter_special' => array(
467+
'content' => 'newsletter_special',
468+
'term' => 'newsletter_special',
469+
),
470+
'social_campaign' => array(
471+
'content' => 'social_campaign',
472+
'term' => 'social_campaign',
473+
),
474+
'influencer_collab' => array(
475+
'content' => 'influencer_collab',
476+
'term' => 'influencer_collab',
477+
),
478+
);
479+
480+
$campaign_name = array_rand( $campaigns );
481+
$campaign = $campaigns[ $campaign_name ];
482+
483+
return array(
484+
'_wc_order_attribution_utm_campaign' => $campaign_name,
485+
'_wc_order_attribution_utm_content' => $campaign['content'],
486+
'_wc_order_attribution_utm_term' => $campaign['term'],
487+
);
488+
}
489+
324490
}

0 commit comments

Comments
 (0)