AAMFieldsExtractor.php
3 years ago
AAMSettingsFields.php
3 years ago
EventIdGenerator.php
3 years ago
FacebookPixel.php
3 years ago
FacebookPluginConfig.php
3 years ago
FacebookPluginUtils.php
3 years ago
FacebookServerSideEvent.php
3 years ago
FacebookWordpressOpenBridge.php
3 years ago
FacebookWordpressOptions.php
3 years ago
FacebookWordpressPixelInjection.php
3 years ago
FacebookWordpressSettingsPage.php
3 years ago
FacebookWordpressSettingsRecorder.php
3 years ago
PixelRenderer.php
3 years ago
ServerEventAsyncTask.php
3 years ago
ServerEventFactory.php
3 years ago
AAMFieldsExtractor.php
85 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (C) 2015-present, Meta, Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | namespace FacebookPixelPlugin\Core; |
| 17 | |
| 18 | use FacebookAds\Object\ServerSide\Normalizer; |
| 19 | |
| 20 | final class AAMFieldsExtractor { |
| 21 | /** |
| 22 | * Filters the passed user data using the AAM settings of the pixel |
| 23 | * @param string[] $user_data_array |
| 24 | * @return string[] |
| 25 | */ |
| 26 | public static function getNormalizedUserData($user_data_array) { |
| 27 | $aam_setttings = FacebookWordpressOptions::getAAMSettings(); |
| 28 | if(!$user_data_array || !$aam_setttings || |
| 29 | !$aam_setttings->getEnableAutomaticMatching()){ |
| 30 | return array(); |
| 31 | } |
| 32 | |
| 33 | //Removing fields not enabled in AAM settings |
| 34 | foreach ($user_data_array as $key => $value) { |
| 35 | if(!in_array($key, $aam_setttings->getEnabledAutomaticMatchingFields())){ |
| 36 | unset($user_data_array[$key]); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Normalizing gender and date of birth |
| 41 | // According to https://developers.facebook.com/docs/facebook-pixel/advanced/advanced-matching |
| 42 | if( |
| 43 | array_key_exists(AAMSettingsFields::GENDER, $user_data_array) |
| 44 | && !empty($user_data_array[AAMSettingsFields::GENDER]) |
| 45 | ){ |
| 46 | $user_data_array[AAMSettingsFields::GENDER] = |
| 47 | $user_data_array[AAMSettingsFields::GENDER][0]; |
| 48 | } |
| 49 | if( |
| 50 | array_key_exists(AAMSettingsFields::DATE_OF_BIRTH, $user_data_array) |
| 51 | ){ |
| 52 | // strtotime() and date() return false for invalid parameters |
| 53 | $unix_timestamp = |
| 54 | strtotime($user_data_array[AAMSettingsFields::DATE_OF_BIRTH]); |
| 55 | if(!$unix_timestamp){ |
| 56 | unset($user_data_array[AAMSettingsFields::DATE_OF_BIRTH]); |
| 57 | } else { |
| 58 | $formatted_date = date("Ymd", $unix_timestamp); |
| 59 | if(!$formatted_date){ |
| 60 | unset($user_data_array[AAMSettingsFields::DATE_OF_BIRTH]); |
| 61 | } else { |
| 62 | $user_data_array[AAMSettingsFields::DATE_OF_BIRTH] = $formatted_date; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | // Given that the format of advanced matching fields is the same in |
| 67 | // the Pixel and the Conversions API, |
| 68 | // we can use the business sdk for normalization |
| 69 | // Compare the documentation: |
| 70 | // https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/customer-information-parameters |
| 71 | // https://developers.facebook.com/docs/facebook-pixel/advanced/advanced-matching |
| 72 | foreach($user_data_array as $field => $data){ |
| 73 | try{ |
| 74 | $normalized_value = Normalizer::normalize($field, $data); |
| 75 | $user_data_array[$field] = $normalized_value; |
| 76 | } |
| 77 | catch(\Exception $e){ |
| 78 | unset($user_data_array[$field]); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $user_data_array; |
| 83 | } |
| 84 | } |
| 85 |