Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/utils/Metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,32 @@
tauriGAEvents.clear();
}

// GA4 derives campaign attribution by parsing `page_location`. We deliberately
// do not send the real URL (it can carry project paths and file names), so an
// inbound `?utm_source=...` handoff from phcode.io would otherwise be invisible
// and every such session would be reported as direct/none. Allow-list only the
// six standard campaign keys back into `page_location`; everything else in the
// query string is still dropped.
function _getCampaignQuery() {
const campaignParams = ['utm_source', 'utm_medium', 'utm_campaign',
'utm_content', 'utm_term', 'utm_id'];
try {
const incoming = new URLSearchParams(window.location.search);
const allowed = new URLSearchParams();
for(let i = 0; i < campaignParams.length; i++){
const key = campaignParams[i];
if(incoming.has(key)){
allowed.set(key, incoming.get(key));
}
}

Check warning on line 262 in src/utils/Metrics.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Expected a `for-of` loop instead of a `for` loop with this simple iteration.

See more on https://sonarcloud.io/project/issues?id=phcode-dev_phoenix&issues=AaAqEbdpCOb4kPNIcADw&open=AaAqEbdpCOb4kPNIcADw&pullRequest=3128
const queryString = allowed.toString();
return queryString ? ('?' + queryString) : '';
} catch (e) {
// never let attribution break analytics init
return '';
}

Check warning on line 268 in src/utils/Metrics.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this exception or don't catch it at all.

See more on https://sonarcloud.io/project/issues?id=phcode-dev_phoenix&issues=AaAqEbdpCOb4kPNIcADx&open=AaAqEbdpCOb4kPNIcADx&pullRequest=3128
}

function _initGoogleAnalytics() {
// Load google analytics scripts
if(Phoenix.isNativeApp) {
Expand All @@ -262,10 +288,10 @@
gtag('config', brackets.config.googleAnalyticsID, {
'page_title': 'Phoenix editor',
'page_path': '/index.html',
'page_location': window.location.origin
'page_location': window.location.origin + _getCampaignQuery()
});
};
script.src = 'https://www.googletagmanager.com/gtag/js?' + brackets.config.googleAnalyticsID;
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + brackets.config.googleAnalyticsID;
document.getElementsByTagName('head')[0].appendChild(script);
}

Expand Down
Loading