actions.js
5 days ago
booking.js
5 days ago
cabinet.js
5 days ago
cart.js
5 days ago
catalog.js
5 days ago
coupon.js
5 days ago
customFields.js
5 days ago
events.js
5 days ago
facebookPixel.js
5 days ago
ivy.js
5 days ago
package.js
5 days ago
panel.js
5 days ago
public.js
5 days ago
renderActions.js
5 days ago
restore.js
5 days ago
slots.js
5 days ago
translation.js
5 days ago
user.js
5 days ago
actions.js
349 lines
| 1 | import { event as gtEvent } from 'vue-gtag' |
| 2 | import { event as fpEvent } from './facebookPixel' |
| 3 | import { useCartItem } from './cart' |
| 4 | import { useAppointmentBookingData } from './booking' |
| 5 | import { useSecondsToDuration } from '../common/date' |
| 6 | |
| 7 | function trackAmeliaData(data, marketing, type, action) { |
| 8 | Object.keys(marketing).forEach((analytics) => { |
| 9 | if (!marketing[analytics] || !window.wpAmeliaSettings[analytics].id) { |
| 10 | return |
| 11 | } |
| 12 | |
| 13 | // Check if Google Analytics feature is enabled |
| 14 | if ( |
| 15 | (analytics === 'googleAnalytics' || analytics === 'googleTag') && |
| 16 | !window.wpAmeliaSettings.featuresIntegrations.googleAnalytics.enabled |
| 17 | ) { |
| 18 | return |
| 19 | } |
| 20 | |
| 21 | // Check if Facebook Pixel feature is enabled |
| 22 | if ( |
| 23 | analytics === 'facebookPixel' && |
| 24 | !window.wpAmeliaSettings.featuresIntegrations.facebookPixel.enabled |
| 25 | ) { |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | let settings = |
| 30 | type in window.wpAmeliaSettings[analytics].tracking |
| 31 | ? window.wpAmeliaSettings[analytics].tracking[type] |
| 32 | : [] |
| 33 | |
| 34 | settings |
| 35 | .filter((trackingEvent) => trackingEvent.type === action) |
| 36 | .forEach((trackingEvent) => { |
| 37 | let trackingEventProperties = {} |
| 38 | |
| 39 | let trackingEventData = {} |
| 40 | |
| 41 | switch (analytics) { |
| 42 | case 'googleTag': { |
| 43 | trackingEventData = trackingEvent |
| 44 | |
| 45 | break |
| 46 | } |
| 47 | |
| 48 | case 'googleAnalytics': |
| 49 | case 'facebookPixel': { |
| 50 | trackingEvent.data.forEach((item) => { |
| 51 | if (item.key.trim() !== '' && item.value.trim() !== '') { |
| 52 | trackingEventData[item.key] = item.value |
| 53 | } |
| 54 | }) |
| 55 | |
| 56 | break |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | Object.keys(trackingEventData) |
| 61 | .filter( |
| 62 | (property) => |
| 63 | analytics === 'googleAnalytics' || |
| 64 | analytics === 'facebookPixel' || |
| 65 | property !== 'type', |
| 66 | ) |
| 67 | .forEach((property) => { |
| 68 | let originalValue = trackingEventData[property] |
| 69 | |
| 70 | let itemProperties = originalValue.replace(/\s\s+/g, ' ').split('%').join('').split(' ') |
| 71 | |
| 72 | itemProperties.forEach((value) => { |
| 73 | if ( |
| 74 | value && |
| 75 | !Array.isArray(value) && |
| 76 | !(typeof value === 'object') && |
| 77 | value !== true |
| 78 | ) { |
| 79 | let pathParts = value.split('_') |
| 80 | |
| 81 | let referenceObject = |
| 82 | pathParts[0] === 'window' ? window : data === null ? window : data |
| 83 | |
| 84 | if (pathParts.length > 1) { |
| 85 | if (value.includes('custom_field')) { |
| 86 | let fieldId = pathParts[pathParts.length - 1] |
| 87 | pathParts = ['booking', 'customFields', fieldId, 'value'] |
| 88 | } |
| 89 | |
| 90 | if (value.includes('appointment_duration')) { |
| 91 | pathParts = ['appointments', 0, 'duration'] |
| 92 | } |
| 93 | |
| 94 | if (value.includes('number_of_persons')) { |
| 95 | if ( |
| 96 | data && |
| 97 | data.appointments && |
| 98 | data.appointments[0] && |
| 99 | data.appointments[0].persons !== null && |
| 100 | typeof data.appointments[0].persons !== 'undefined' |
| 101 | ) { |
| 102 | pathParts = ['appointments', 0, 'persons'] |
| 103 | } else if ( |
| 104 | data && |
| 105 | data.booking && |
| 106 | data.booking.persons !== null && |
| 107 | typeof data.booking.persons !== 'undefined' |
| 108 | ) { |
| 109 | pathParts = ['booking', 'persons'] |
| 110 | } else if ( |
| 111 | data && |
| 112 | data.number_of_persons !== null && |
| 113 | typeof data.number_of_persons !== 'undefined' |
| 114 | ) { |
| 115 | pathParts = ['number_of_persons'] |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | pathParts.forEach((pathPart) => { |
| 120 | if ( |
| 121 | typeof referenceObject !== 'undefined' && |
| 122 | pathPart in referenceObject && |
| 123 | referenceObject[pathPart] !== null |
| 124 | ) { |
| 125 | referenceObject = referenceObject[pathPart] |
| 126 | } else { |
| 127 | return false |
| 128 | } |
| 129 | }) |
| 130 | |
| 131 | if (!Array.isArray(referenceObject) && !(typeof referenceObject === 'object')) { |
| 132 | if ( |
| 133 | value === 'payment_amount' && |
| 134 | referenceObject.toString().endsWith('.00') && |
| 135 | analytics === 'googleTag' && |
| 136 | property === 'value' |
| 137 | ) { |
| 138 | referenceObject = parseInt(referenceObject) |
| 139 | } |
| 140 | |
| 141 | if (value === 'appointment_duration') { |
| 142 | referenceObject = useSecondsToDuration( |
| 143 | referenceObject, |
| 144 | window.wpAmeliaLabels['h'], |
| 145 | window.wpAmeliaLabels['min'], |
| 146 | ) |
| 147 | } |
| 148 | |
| 149 | originalValue = originalValue |
| 150 | .split('%' + value + '%') |
| 151 | .join(referenceObject) |
| 152 | .trim() |
| 153 | } else { |
| 154 | originalValue = originalValue |
| 155 | .split('%' + value + '%') |
| 156 | .join('') |
| 157 | .trim() |
| 158 | } |
| 159 | } else if (typeof referenceObject !== 'undefined' && value in referenceObject) { |
| 160 | if ( |
| 161 | !Array.isArray(referenceObject[value]) && |
| 162 | !(typeof referenceObject[value] === 'object') |
| 163 | ) { |
| 164 | if ( |
| 165 | value === 'payment_amount' && |
| 166 | referenceObject[value].endsWith('.00') && |
| 167 | analytics === 'googleTag' && |
| 168 | property === 'value' |
| 169 | ) { |
| 170 | referenceObject[value] = parseInt(referenceObject[value]) |
| 171 | } |
| 172 | |
| 173 | originalValue = originalValue |
| 174 | .split('%' + value + '%') |
| 175 | .join(referenceObject[value]) |
| 176 | .trim() |
| 177 | } else { |
| 178 | originalValue = originalValue |
| 179 | .split('%' + value + '%') |
| 180 | .join('') |
| 181 | .trim() |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | }) |
| 186 | |
| 187 | trackingEventProperties[property] = originalValue |
| 188 | }) |
| 189 | |
| 190 | switch (analytics) { |
| 191 | case 'googleTag': { |
| 192 | gtEvent(trackingEventProperties.action, { |
| 193 | event_category: trackingEventProperties.category, |
| 194 | event_label: trackingEventProperties.label, |
| 195 | value: trackingEventProperties.value, |
| 196 | }) |
| 197 | |
| 198 | break |
| 199 | } |
| 200 | |
| 201 | case 'googleAnalytics': { |
| 202 | gtEvent(trackingEvent.event, trackingEventProperties) |
| 203 | |
| 204 | break |
| 205 | } |
| 206 | |
| 207 | case 'facebookPixel': { |
| 208 | fpEvent(trackingEvent.event, trackingEventProperties) |
| 209 | |
| 210 | break |
| 211 | } |
| 212 | } |
| 213 | }) |
| 214 | }) |
| 215 | } |
| 216 | |
| 217 | function useAction(store, additionalData, action, type, successCallback, errorCallback) { |
| 218 | let data = {} |
| 219 | |
| 220 | switch (type) { |
| 221 | case 'appointment': |
| 222 | if (additionalData && additionalData.isCartAppointment) { |
| 223 | data.service = additionalData.serviceId |
| 224 | ? store.getters['entities/getService'](additionalData.serviceId) |
| 225 | : null |
| 226 | data.employee = additionalData.providerId |
| 227 | ? store.getters['entities/getEmployee'](additionalData.providerId) |
| 228 | : null |
| 229 | data.location = additionalData.locationId |
| 230 | ? store.getters['entities/getLocation'](additionalData.locationId) |
| 231 | : null |
| 232 | |
| 233 | data.category = data.service |
| 234 | ? store.getters['entities/getCategory'](data.service.categoryId) |
| 235 | : null |
| 236 | |
| 237 | if (data.employee) { |
| 238 | data.employee.fullName = data.employee.firstName + ' ' + data.employee.lastName |
| 239 | } |
| 240 | } else { |
| 241 | data.service = store.getters['booking/getServiceId'] |
| 242 | ? store.getters['entities/getService'](store.getters['booking/getServiceId']) |
| 243 | : null |
| 244 | |
| 245 | data.employee = store.getters['booking/getEmployeeId'] |
| 246 | ? store.getters['entities/getEmployee'](store.getters['booking/getEmployeeId']) |
| 247 | : null |
| 248 | |
| 249 | if (data.employee) { |
| 250 | data.employee.fullName = data.employee.firstName + ' ' + data.employee.lastName |
| 251 | } |
| 252 | |
| 253 | data.location = store.getters['booking/getLocationId'] |
| 254 | ? store.getters['entities/getLocation'](store.getters['booking/getLocationId']) |
| 255 | : null |
| 256 | |
| 257 | data.category = store.getters['booking/getCategoryId'] |
| 258 | ? store.getters['entities/getCategory'](store.getters['booking/getCategoryId']) |
| 259 | : null |
| 260 | |
| 261 | if (!data.category) { |
| 262 | data.category = data.service |
| 263 | ? store.getters['entities/getCategory'](data.service.categoryId) |
| 264 | : null |
| 265 | } |
| 266 | |
| 267 | if (data.service) { |
| 268 | let cartItem = useCartItem(store) |
| 269 | |
| 270 | if ( |
| 271 | cartItem && |
| 272 | 'services' in cartItem && |
| 273 | data.service.id in cartItem.services && |
| 274 | cartItem.services[data.service.id].list.length |
| 275 | ) { |
| 276 | if (!data.employee) { |
| 277 | data.employee = cartItem.services[data.service.id].list[0].providerId |
| 278 | ? store.getters['entities/getEmployee']( |
| 279 | cartItem.services[data.service.id].list[0].providerId, |
| 280 | ) |
| 281 | : null |
| 282 | } |
| 283 | |
| 284 | if (data.employee) { |
| 285 | data.employee.fullName = data.employee.firstName + ' ' + data.employee.lastName |
| 286 | } |
| 287 | |
| 288 | if (!data.location) { |
| 289 | data.location = cartItem.services[data.service.id].list[0].locationId |
| 290 | ? store.getters['entities/getLocation']( |
| 291 | cartItem.services[data.service.id].list[0].locationId, |
| 292 | ) |
| 293 | : null |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | data.booking = store.getters['booking/getBooking'] |
| 299 | |
| 300 | data.appointments = useAppointmentBookingData(store) |
| 301 | } |
| 302 | |
| 303 | break |
| 304 | |
| 305 | case 'package': |
| 306 | data.package = store.getters['entities/getPackage'] |
| 307 | ? store.getters['entities/getPackage'](store.getters['booking/getPackageId']) |
| 308 | : null |
| 309 | |
| 310 | data.booking = store.getters['booking/getBooking'] |
| 311 | //data.appointments = usePackageBookingData(store) |
| 312 | |
| 313 | break |
| 314 | |
| 315 | case 'event': { |
| 316 | const customFieldsData = store.getters['customFields/getAllData'] |
| 317 | data.booking = { |
| 318 | customer: store.getters['customerInfo/getAllData'], |
| 319 | customFields: Object.values( |
| 320 | customFieldsData && customFieldsData.customFields ? customFieldsData.customFields : {}, |
| 321 | ), |
| 322 | persons: store.getters['persons/getPersons'], |
| 323 | } |
| 324 | data.event = |
| 325 | additionalData && additionalData.event |
| 326 | ? additionalData.event |
| 327 | : store.getters['eventEntities/getEvent']( |
| 328 | store.getters['eventBooking/getSelectedEventId'], |
| 329 | ) |
| 330 | break |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | trackAmeliaData( |
| 335 | Object.assign(data, additionalData), |
| 336 | { facebookPixel: true, googleAnalytics: true, googleTag: true }, |
| 337 | type, |
| 338 | action, |
| 339 | ) |
| 340 | |
| 341 | if ('ameliaActions' in window && action in window.ameliaActions) { |
| 342 | window.ameliaActions[action](successCallback, errorCallback, data) |
| 343 | } else if (successCallback !== null) { |
| 344 | successCallback() |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | export default useAction |
| 349 |