|
112 | 112 | return _; |
113 | 113 | }]) |
114 | 114 |
|
| 115 | + /* |
| 116 | + * A factory which is used to track analytics actions |
| 117 | + */ |
| 118 | + .factory('trackAnalyticsActions', ['MEDIUM_TYPES', function (MEDIUM_TYPES) { |
| 119 | + let analyticsTrackingInterval = null; |
| 120 | + let lastAnalyticsTime = null; |
| 121 | + let openedItems = []; |
| 122 | + let playedItems = []; |
| 123 | + |
| 124 | + function trackPlayedItem(options) { |
| 125 | + const { item, itemType } = options; |
| 126 | + |
| 127 | + const metaData = { |
| 128 | + itemId: item.guid, |
| 129 | + itemTitle: item.title, |
| 130 | + imageUrl: item.imageSrcUrl, |
| 131 | + }; |
| 132 | + |
| 133 | + if (!playedItems.includes(item.guid)) { |
| 134 | + playedItems.push(item.guid); |
| 135 | + AnalyticsManager.trackEvent(`${item.guid}_playsCount`, metaData); |
| 136 | + if (itemType === 'video') { |
| 137 | + AnalyticsManager.trackEvent(`videoPlaysCount`, metaData); |
| 138 | + } else { |
| 139 | + AnalyticsManager.trackEvent(`audioPlaysCount`, metaData); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + function trackOpenedItem(item) { |
| 145 | + if (!openedItems.includes(item.guid)) { |
| 146 | + openedItems.push(item.guid); |
| 147 | + const metaData = { |
| 148 | + itemId: item.guid, |
| 149 | + itemTitle: item.title, |
| 150 | + imageUrl: item.imageSrcUrl, |
| 151 | + }; |
| 152 | + AnalyticsManager.trackEvent(`${item.guid}_opensCount`, metaData); |
| 153 | + switch (item.type) { |
| 154 | + case MEDIUM_TYPES.VIDEO: |
| 155 | + AnalyticsManager.trackEvent('videoOpensCount', metaData); |
| 156 | + break; |
| 157 | + case MEDIUM_TYPES.AUDIO: |
| 158 | + AnalyticsManager.trackEvent('audioOpensCount', metaData); |
| 159 | + break; |
| 160 | + default: |
| 161 | + AnalyticsManager.trackEvent('articleOpensCount', metaData); |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + function trackItemWatchState(options) { |
| 168 | + const { state, currentTime, item, itemType } = options; |
| 169 | + |
| 170 | + const metaData = { |
| 171 | + itemId: item.guid, |
| 172 | + itemTitle: item.title, |
| 173 | + imageUrl: item.imageSrcUrl, |
| 174 | + }; |
| 175 | + const eventKey = `${item.guid}_secondsWatch`; |
| 176 | + if (state === 'play') { |
| 177 | + trackPlayedItem({ item, itemType }); |
| 178 | + if (currentTime) { |
| 179 | + lastAnalyticsTime = currentTime; |
| 180 | + } |
| 181 | + |
| 182 | + if (!analyticsTrackingInterval) { |
| 183 | + analyticsTrackingInterval = setInterval(() => { |
| 184 | + lastAnalyticsTime += 5; |
| 185 | + metaData._buildfire = { aggregationValue: 5 }; // 5 seconds |
| 186 | + AnalyticsManager.trackEvent(eventKey, metaData); |
| 187 | + }, 5 * 1000); |
| 188 | + } |
| 189 | + } else if (state === 'pause') { |
| 190 | + if (analyticsTrackingInterval) { |
| 191 | + clearInterval(analyticsTrackingInterval); |
| 192 | + analyticsTrackingInterval = null; |
| 193 | + |
| 194 | + const extraTime = parseInt(currentTime - lastAnalyticsTime); |
| 195 | + lastAnalyticsTime = currentTime; |
| 196 | + if (currentTime > 0 && extraTime > 0 && extraTime <= 5) { |
| 197 | + metaData._buildfire = { aggregationValue: extraTime }; |
| 198 | + AnalyticsManager.trackEvent(eventKey, metaData); |
| 199 | + } |
| 200 | + } |
| 201 | + } else if (state === 'buffer' && currentTime) { |
| 202 | + lastAnalyticsTime = currentTime; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return { |
| 207 | + trackPlayedItem, |
| 208 | + trackOpenedItem, |
| 209 | + trackItemWatchState |
| 210 | + } |
| 211 | + }]) |
| 212 | + |
| 213 | + .factory('utils', ['$filter' , function ($filter) { |
| 214 | + /** |
| 215 | + * @name getImageUrl() |
| 216 | + * Used to extract image url |
| 217 | + * @param item |
| 218 | + * @returns {*} |
| 219 | + */ |
| 220 | + function getImageUrl(item) { |
| 221 | + var i = 0, |
| 222 | + length = 0, |
| 223 | + imageUrl = ''; |
| 224 | + if (item.image && item.image.url) { |
| 225 | + imageUrl = item.image.url; |
| 226 | + } else if (item.enclosures && item.enclosures.length > 0) { |
| 227 | + length = item.enclosures.length; |
| 228 | + for (i = 0; i < length; i++) { |
| 229 | + if (item.enclosures[i].type.indexOf('image') === 0 || item.enclosures[i].type.indexOf('img') != -1) { |
| 230 | + imageUrl = item.enclosures[i].url; |
| 231 | + break; |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + if (imageUrl) { |
| 236 | + return imageUrl; |
| 237 | + } |
| 238 | + else { |
| 239 | + if (item['media:thumbnail'] && item['media:thumbnail']['@'] && item['media:thumbnail']['@'].url) { |
| 240 | + return item['media:thumbnail']['@'].url; |
| 241 | + } else if (item['media:group'] && item['media:group']['media:content'] && item['media:group']['media:content']['media:thumbnail'] && item['media:group']['media:content']['media:thumbnail']['@'] && item['media:group']['media:content']['media:thumbnail']['@'].url) { |
| 242 | + return item['media:group']['media:content']['media:thumbnail']['@'].url; |
| 243 | + } else if (item.description) { |
| 244 | + return $filter('extractImgSrc')(item.description); |
| 245 | + } else { |
| 246 | + return ''; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return { getImageUrl } |
| 252 | + }]) |
| 253 | + |
115 | 254 | /** |
116 | 255 | * A factory which is used to hold selected item before going on item details page. |
117 | 256 | */ |
|
0 commit comments