fbpx

Donate Now

const getUrl = require('getUrl'); const sendPixel = require('sendPixel'); const setInWindow = require('setInWindow'); const getTimestamp = require('getTimestamp'); const injectScript = require('injectScript'); const copyFromWindow = require('copyFromWindow'); const encodeUriComponent = require('encodeUriComponent'); const callLater = require('callLater'); /** * Globals */ const conversionIds = data.conversionId ? data.conversionId.split(',').slice(0,3).map(id => id.trim()) : ''; const allPids = []; const pageUrl = data.customUrl ? data.customUrl : getUrl(); const eventId = data.eventId; let isScriptInjected = false; /** * Checks presence of LinkedIn Insight tag code. */ const isInsightTagAPIAvailable = () => typeof copyFromWindow('lintrk') === 'function'; /** * Reads and Sets in global namespace all applicable PIDs on the page */ const setAllPids = (function() { const partnerIds = {}; const bizoId = copyFromWindow('_bizo_data_partner_id'); const bizoIds = copyFromWindow('_bizo_data_partner_ids') || []; const linkedInPartnerId = copyFromWindow('_linkedin_data_partner_id'); const linkedInPartnerIds = copyFromWindow('_linkedin_data_partner_ids') || []; const addPid = pid => { if (pid && !partnerIds[pid]) { partnerIds[pid] = true; allPids.push(pid); } }; // add the partner ids set via this GTM installation const inputPids = data.partnerId.split(','); inputPids.forEach(id => addPid(id.trim())); // Add all PIDs that may have updated the global, helps skipping following adds. addPid(linkedInPartnerId); linkedInPartnerIds.forEach(id => addPid(id)); // add other PIDs from page addPid(bizoId); bizoIds.forEach(id => addPid(id)); // Update the main namespace for future tracking by InsightTag to include the partner IDs setInWindow('_linkedin_data_partner_ids', allPids, true); }()); /** * Generate query params only for GTM based tracking */ function generateQueryParamsForGTM(cid) { const encodedPIDs = encodeUriComponent(allPids.join(',')); let result = 'pid=' + encodedPIDs; result += '&tm=gtmv2'; result += cid ? '&conversionId=' + encodeUriComponent(cid) : ''; result += '&url=' + encodeUriComponent(pageUrl); result += eventId ? '&eventId=' + encodeUriComponent(eventId) : ''; result += '&v=2&fmt=js&time=' + getTimestamp(); return result; } // Success call back to InsightTag injection function didInjectInsightTag() { callLater(() => { trackByInsightTag(); }); } // Callback to plain GTM when InsightTag code failed to inject function didFailInsightTag() { trackByPlainGTM(); } function trackByPlainGTM() { if (conversionIds.length && conversionIds.length <= 3) { conversionIds.forEach(id => { const trackingUrl = 'https://px.ads.linkedin.com/collect?' + generateQueryParamsForGTM(id); sendPixel(trackingUrl, data.gtmOnSuccess, data.gtmOnFailure); }); } else { sendPixel('https://px.ads.linkedin.com/collect?' + generateQueryParamsForGTM(), data.gtmOnSuccess, data.gtmOnFailure); } } /** * Download LinkedIn Insight tag core code if `window.lintrk` is not available * Also ensure it doesn’t default fire. */ function trackByInsightTag() { if (isInsightTagAPIAvailable()) { const lintrk = copyFromWindow('lintrk'); const options = { tmsource: 'gtmv2' }; options.conversion_url = pageUrl; if (eventId) { options.event_id = eventId; } if (conversionIds.length && conversionIds.length <= 3) { conversionIds.forEach(id => { options.conversion_id = id; lintrk('track', options); }); } else { lintrk('track', options); } data.gtmOnSuccess(); } else if (!isScriptInjected) { isScriptInjected = true; setInWindow('_already_called_lintrk', true, true); injectScript('https://snap.licdn.com/li.lms-analytics/insight.min.js', didInjectInsightTag, didFailInsightTag); } else { didInjectInsightTag(); } } trackByInsightTag();