{"version":3,"file":"segmentAnalytics-6ac551bf.js","sources":["../../../../app/frontend/components/shared/tracking/segmentAnalytics.js"],"sourcesContent":["/* global analytics:false */\nconst RESERVED_ATTRIBUTES = ['event', 'view-event', 'load-event', 'load-action', 'view-action', 'manual-view', 'manual-load']\n\nfunction getElementProperties(el) {\n const dataset = el.dataset;\n\n\n return {\n 'app_type': dataset.appType,\n\n // cta specific\n 'cta_type': dataset.ctaType,\n 'cta_location': dataset.ctaLocation,\n 'cta_topic': dataset.ctaTopic,\n 'cta_version': dataset.ctaVersion,\n 'cta_template': dataset.ctaTemplate,\n\n\n // directory data\n\n 'location_id': dataset.idHash,\n 'is_solv_partner': dataset.isSolvPartner === 'true' || undefined,\n 'is_bookable': dataset.isBookable === 'true' || undefined,\n 'is_viewable': dataset.isViewable,\n 'platform_type': dataset.platformType,\n 'city': dataset.directoryCity,\n 'state': dataset.directoryState,\n 'version': dataset.version,\n 'listing_card_rank': dataset.cardRank,\n\n // article data\n 'article_title': dataset.articleTitle,\n 'date_published': dataset.articlePublished,\n 'date_updated': dataset.articleUpdated,\n 'image': dataset.articleImage,\n 'author': dataset.articleAuthor,\n 'word_count': dataset.articleWordCount,\n 'specialty': dataset.articleSpecialty,\n\n // link data\n 'link_topic': dataset.linkTopic,\n 'link_type': dataset.linkType,\n 'link_text': dataset.linkText,\n 'link_location': dataset.linkLocation,\n 'link_color': dataset.linkColor,\n 'link_address': dataset.linkAddress,\n ...getSegmentGlobbedProperties(el),\n }\n\n}\n\nfunction getContextProperties(event) {\n const bodyDataset = document.body.dataset;\n\n let segmentGlobalProperties = getSegmentGlobbedProperties(document.body);\n let directoryContents = getDirectoryContents(bodyDataset, event);\n let updatedGlobalProperties = updateGlobalProperties(segmentGlobalProperties, directoryContents);\n\n return {\n 'isMobile': window.innerWidth < 769,\n ...updatedGlobalProperties,\n ...directoryContents,\n }\n}\n\n// This will now pick up all attributes that start with \"data-segment\" and snake_case them for return\n// We only need to explicitly define attributes on the dataset that cannot match this format\nfunction getSegmentGlobbedProperties(el) {\n\n const globbedProperties = {};\n\n // TODO: refactor the views to hold to this format\n for (const key of el.getAttributeNames()) {\n if (key.startsWith('data-segment-')) {\n const rawKey = key.replace('data-segment-', '');\n if (RESERVED_ATTRIBUTES.includes(rawKey)) {\n continue\n } else {\n const snakeCaseKey = rawKey.replaceAll('-', '_');\n globbedProperties[snakeCaseKey] = el.getAttribute(key);\n }\n }\n }\n return globbedProperties;\n\n}\n\n// gets object of directory results for directory load event\nfunction getDirectoryContents(dataset, event) {\n const eventsWithResultsContain = [\n \"Directory - Load\",\n \"Directory - Slots\",\n \"Directory - CDP Card\",\n \"Directory - CTA\",\n \"Directory - Profile Card\"\n ];\n if (eventsWithResultsContain.includes(event)) {\n let results_contain = {\n partner_count: dataset.segmentPartnerCount ||= \"0\",\n nonpartner_count: dataset.segmentNonpartnerCount ||= \"0\",\n nearby_bookable_count: dataset.segmentNearbyBookableCount ||= \"0\",\n nearby_results_count: dataset.segmentNearbyResultsCount ||= \"0\",\n telemed_count: dataset.segmentTelemedCount ||= \"0\",\n }\n let affiliate_partnerships = {\n growtherapy: dataset.segmentGrowtherapy ||= \"0\",\n quest_health: dataset.segmentQuestHealth ||= \"0\",\n labcorp: dataset.segmentLabcorp ||= \"0\",\n }\n\n return {results_contain: results_contain, affiliate_partnerships: affiliate_partnerships}\n\n } else {\n return {}\n }\n}\n\nfunction updateGlobalProperties(globalProperties, directoryContents) {\n if (directoryContents.results_contain) {\n delete globalProperties.partner_count;\n delete globalProperties.nonpartner_count;\n delete globalProperties.nearby_bookable_count;\n delete globalProperties.nearby_results_count;\n delete globalProperties.telemed_count;\n }\n if (directoryContents.affiliate_partnerships) {\n delete globalProperties.growtherapy;\n delete globalProperties.quest_health;\n delete globalProperties.labcorp;\n }\n\n return globalProperties;\n}\n\nfunction trackDOMEvent(e, custom_properties = {}) {\n const el = e.currentTarget;\n if (el.dataset.preventSegmentTracking !== undefined) {\n return;\n }\n const event = el.dataset.segmentEvent;\n const listingProperties = getElementProperties(el);\n const contextProperties = getContextProperties(event);\n\n return trackSegmentEvent(event, {...listingProperties, ...contextProperties, ..._removeUndefined(custom_properties)});\n}\n\nfunction trackLoadEvent(el) {\n const event = el.dataset.segmentLoadEvent\n const action = el.dataset.segmentLoadAction || 'load';\n const listingProperties = getElementProperties(el, event);\n const contextProperties = getContextProperties(event);\n const updatedListingProperties = updateGlobalProperties(listingProperties, contextProperties);\n\n return trackSegmentEvent(event, {...updatedListingProperties, ...contextProperties, action});\n}\n\nfunction trackViewEvent(el) {\n const event = el.dataset.segmentViewEvent;\n const action = el.dataset.segmentViewAction || 'view';\n const listingProperties = getElementProperties(el);\n const contextProperties = getContextProperties(event);\n const updatedListingProperties = updateGlobalProperties(listingProperties, contextProperties);\n\n return trackSegmentEvent(event, {...updatedListingProperties, ...contextProperties, action});\n}\n\nfunction trackSegmentEvent(event, properties, options, callback) {\n if (typeof analytics !== 'undefined') {\n analytics.track(event, _removeUndefined((properties)), options, callback);\n } else {\n console.debug('Not tracking event as Segment Analytics is not loaded', event, _removeUndefined((properties)));\n }\n}\n\nfunction getAnonymousId() {\n try {\n if (typeof analytics !== 'undefined') {\n return analytics.user().anonymousId();\n } else {\n return null;\n }\n } catch (e) {\n return null\n }\n}\n\nfunction getDistinctId() {\n let distinctId;\n\n // Iterate over all local storage items\n for (let i = 0; i < localStorage.length; i++) {\n const key = localStorage.key(i);\n if (key && key.includes('mp_') && key.endsWith('_mixpanel')) {\n const data = localStorage.getItem(key);\n if (data) {\n try {\n const parsedData = JSON.parse(data);\n if (parsedData && parsedData.distinct_id) {\n distinctId = parsedData.distinct_id;\n break;\n }\n } catch (e) {\n }\n }\n }\n }\n\n return distinctId?.toLowerCase() !== 'undefined' ? distinctId : undefined;\n}\n\n\nfunction _removeUndefined(obj) {\n Object.keys(obj).forEach(key => obj[key] === undefined ? delete obj[key] : {})\n return obj\n}\n\nexport {\n trackDOMEvent,\n trackViewEvent,\n trackSegmentEvent,\n trackLoadEvent,\n getContextProperties,\n getElementProperties,\n getAnonymousId,\n getDistinctId\n}\n"],"names":["RESERVED_ATTRIBUTES","getElementProperties","el","dataset","getSegmentGlobbedProperties","getContextProperties","event","bodyDataset","segmentGlobalProperties","directoryContents","getDirectoryContents","updatedGlobalProperties","updateGlobalProperties","globbedProperties","key","rawKey","snakeCaseKey","results_contain","affiliate_partnerships","globalProperties","trackDOMEvent","custom_properties","listingProperties","contextProperties","trackSegmentEvent","_removeUndefined","trackLoadEvent","action","updatedListingProperties","trackViewEvent","properties","options","callback","getAnonymousId","getDistinctId","distinctId","i","data","parsedData","obj"],"mappings":"AACA,MAAMA,EAAsB,CAAC,QAAS,aAAc,aAAc,cAAe,cAAe,cAAe,aAAa,EAE5H,SAASC,EAAqBC,EAAI,CAC9B,MAAMC,EAAUD,EAAG,QAGnB,MAAO,CACH,SAAYC,EAAQ,QAGpB,SAAYA,EAAQ,QACpB,aAAgBA,EAAQ,YACxB,UAAaA,EAAQ,SACrB,YAAeA,EAAQ,WACvB,aAAgBA,EAAQ,YAKxB,YAAeA,EAAQ,OACvB,gBAAmBA,EAAQ,gBAAkB,QAAU,OACvD,YAAeA,EAAQ,aAAe,QAAU,OAChD,YAAeA,EAAQ,WACvB,cAAiBA,EAAQ,aACzB,KAAQA,EAAQ,cAChB,MAASA,EAAQ,eACjB,QAAWA,EAAQ,QACnB,kBAAqBA,EAAQ,SAG7B,cAAiBA,EAAQ,aACzB,eAAkBA,EAAQ,iBAC1B,aAAgBA,EAAQ,eACxB,MAASA,EAAQ,aACjB,OAAUA,EAAQ,cAClB,WAAcA,EAAQ,iBACtB,UAAaA,EAAQ,iBAGrB,WAAcA,EAAQ,UACtB,UAAaA,EAAQ,SACrB,UAAaA,EAAQ,SACrB,cAAiBA,EAAQ,aACzB,WAAcA,EAAQ,UACtB,aAAgBA,EAAQ,YACxB,GAAGC,EAA4BF,CAAE,CACpC,CAEL,CAEA,SAASG,EAAqBC,EAAO,CACjC,MAAMC,EAAc,SAAS,KAAK,Q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