Skip to content

Commit ffe4019

Browse files
committed
chore: ga metrics campaign source analytics
1 parent 42fa984 commit ffe4019

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

src/utils/Metrics.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,32 @@ define(function (require, exports, module) {
242242
tauriGAEvents.clear();
243243
}
244244

245+
// GA4 derives campaign attribution by parsing `page_location`. We deliberately
246+
// do not send the real URL (it can carry project paths and file names), so an
247+
// inbound `?utm_source=...` handoff from phcode.io would otherwise be invisible
248+
// and every such session would be reported as direct/none. Allow-list only the
249+
// six standard campaign keys back into `page_location`; everything else in the
250+
// query string is still dropped.
251+
function _getCampaignQuery() {
252+
const campaignParams = ['utm_source', 'utm_medium', 'utm_campaign',
253+
'utm_content', 'utm_term', 'utm_id'];
254+
try {
255+
const incoming = new URLSearchParams(window.location.search);
256+
const allowed = new URLSearchParams();
257+
for(let i = 0; i < campaignParams.length; i++){
258+
const key = campaignParams[i];
259+
if(incoming.has(key)){
260+
allowed.set(key, incoming.get(key));
261+
}
262+
}
263+
const queryString = allowed.toString();
264+
return queryString ? ('?' + queryString) : '';
265+
} catch (e) {
266+
// never let attribution break analytics init
267+
return '';
268+
}
269+
}
270+
245271
function _initGoogleAnalytics() {
246272
// Load google analytics scripts
247273
if(Phoenix.isNativeApp) {
@@ -262,10 +288,10 @@ define(function (require, exports, module) {
262288
gtag('config', brackets.config.googleAnalyticsID, {
263289
'page_title': 'Phoenix editor',
264290
'page_path': '/index.html',
265-
'page_location': window.location.origin
291+
'page_location': window.location.origin + _getCampaignQuery()
266292
});
267293
};
268-
script.src = 'https://www.googletagmanager.com/gtag/js?' + brackets.config.googleAnalyticsID;
294+
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + brackets.config.googleAnalyticsID;
269295
document.getElementsByTagName('head')[0].appendChild(script);
270296
}
271297

0 commit comments

Comments
 (0)