Skip to content

Commit

Permalink
Merge pull request #82 from getamis/ASL-4736/custom-track-settings
Browse files Browse the repository at this point in the history
[ASL-4736] custom track settings
  • Loading branch information
roadmanfong authored Nov 6, 2023
2 parents bb996cc + 69543fe commit 88ffe37
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const qubicConnect = QubicConnect.initialize({
name: 'Display Name', // will show up in display
key: 'API_KEY',
secret: 'API_SECRET',
enableAutoLoginInWalletIab: false // default true, it will auto login when url opened in Wallet dApp Browser which provides window.ethereum
});
</script>
```
Expand Down Expand Up @@ -88,6 +87,8 @@ const qubicConnect = new QubicConnect({
name: 'Display Name', // will show up in display
key: 'API_KEY',
secret: 'API_SECRET',
enableAutoLoginInWalletIab: false // default true, it will auto login when url opened in Wallet dApp Browser which provides window.ethereum
trackGaSettings: [] // default: [], put measurement ids here to track addPaymentInfo event
});
```

Expand Down
10 changes: 10 additions & 0 deletions examples/example-pure-js/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Qubic Connect JS SDK Example</title>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-W8EB8ZK5CV"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());

gtag('config', 'G-W8EB8ZK5CV');
</script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
4 changes: 2 additions & 2 deletions examples/example-pure-js/src/gqlSchema/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const GET_ASSET_DETAIL = gql`
expiresDays
}
salePhases {
mode
saleMode
}
variants(privateSaleCode: $privateSaleCode) {
id
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface AssetDetail {
expiredDays: string;
} | null;
salePhases: {
mode: string;
saleMode: string;
};
variants: Array<AssetVariant>;
};
Expand Down
1 change: 1 addition & 0 deletions examples/example-pure-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ async function main() {
authRedirectUrl: AUTH_REDIRECT_URL, // optional, for debug
iabRedirectUrl: '', // optional
shouldAlwaysShowCopyUI: false, // optional
trackGaSettings: ['G-W8EB8ZK5CV'],
providerOptions: {
qubic: {
provider: new QubicProvider({
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/QubicConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { buyAsset, AssetBuyResponse, giftRedeem, GiftRedeemResponse } from './ap
import { addLocaleToUrl } from './utils/addLocaleToUrl';
import { clientTicketIssue } from './api/clientTicket';
import { PASS_URL, WALLET_URL } from './constants/config';
import { addTrackSettingsToUrl, initGaTrack } from './utils/addTrackSettingsToUrl';

const DEFAULT_SERVICE_NAME = 'qubic-creator';

Expand Down Expand Up @@ -105,6 +106,7 @@ export class QubicConnect {
shouldAlwaysShowCopyUI = false,
disableOpenExternalBrowserWhenLineIab = false,
enableAutoLoginInWalletIab = true,
trackGaSettings = [],
} = config;
if (!apiKey) {
throw Error('new QubicConnect should have key');
Expand All @@ -126,6 +128,7 @@ export class QubicConnect {
shouldAlwaysShowCopyUI,
disableOpenExternalBrowserWhenLineIab,
enableAutoLoginInWalletIab,
trackGaSettings,
};

QubicConnect.checkProviderOptions(config?.providerOptions);
Expand Down Expand Up @@ -168,6 +171,7 @@ export class QubicConnect {
this.handleRedirectResult();
this.hydrateUser();
this.onAuthStateChanged(QubicConnect.persistUser);
initGaTrack(this.config.trackGaSettings);

if (!this.user && this.shouldAutoLoginInWalletIab) {
this.loginWithWallet(window.ethereum.isQubic ? 'qubic' : 'metamask');
Expand Down Expand Up @@ -595,6 +599,7 @@ export class QubicConnect {
if (options?.locale) {
paymentUrl = addLocaleToUrl(response.assetBuy.paymentUrl, options.locale);
}
paymentUrl = addTrackSettingsToUrl(paymentUrl);

return {
...response,
Expand Down Expand Up @@ -625,6 +630,7 @@ export class QubicConnect {
if (options?.locale) {
paymentUrl = addLocaleToUrl(response.giftRedeem.paymentUrl, options.locale);
}
paymentUrl = addTrackSettingsToUrl(paymentUrl);

return {
...response,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types/QubicConnectConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface QubicConnectConfig {
disableOpenExternalBrowserWhenLineIab?: boolean;
enableAutoLoginInWalletIab?: boolean;

trackGaSettings?: Array<string>;

// # fast login
// authRedirectUrl where have different type of wallet
authRedirectUrl?: string;
Expand Down
58 changes: 58 additions & 0 deletions packages/core/src/utils/addTrackSettingsToUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { gtag } = window as any;

function getClientId(tagId: string): Promise<string> {
return new Promise(resolve => {
if (!gtag) resolve('');

gtag('get', tagId, 'client_id', (clientId: string) => {
resolve(clientId);
});
});
}

function getSessionId(tagId: string): Promise<string> {
return new Promise(resolve => {
if (!gtag) resolve('');
gtag('get', tagId, 'session_id', (sessionId: string) => {
resolve(sessionId);
});
});
}

let cachedGaTrackSettings: string[] = [];
export async function initGaTrack(measurementIds: string[]): Promise<void> {
if (!gtag && measurementIds.length > 0) {
console.warn('window.gtag not found, you should set up gtag in current web to use this function');
return;
}
cachedGaTrackSettings = (
await Promise.all(
measurementIds.map(async measurementId => {
const clientId = await getClientId(measurementId);
const sessionId = await getSessionId(measurementId);
if (!clientId) {
console.error('clientId not found');
return '';
}
if (!sessionId) {
console.error('sessionId not found');
return '';
}
return `${measurementId},${clientId},${sessionId}`;
}),
)
).filter(item => !!item);
}

export function addTrackSettingsToUrl(url: string): string {
const paymentUrlObject = new URL(url);
const params = new URLSearchParams(paymentUrlObject.search);

// https://url?track_ga=setting1&track_ga=setting2
cachedGaTrackSettings.forEach(each => {
params.append('track_ga', each);
});

paymentUrlObject.search = params.toString();
return paymentUrlObject.href;
}

0 comments on commit 88ffe37

Please sign in to comment.