mkfashion-sdk 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/mkfashion.js +59 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mkfashion-sdk",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "SDK para integrar o provador virtual mKFashion com suporte a Wake Commerce",
5
5
  "main": "src/mkfashion.js",
6
6
  "scripts": {
package/src/mkfashion.js CHANGED
@@ -30,6 +30,13 @@ const mkfashion = {
30
30
  apiUrl: 'https://mkfashion-api.mk3dlabs.com',
31
31
  debug: false,
32
32
 
33
+ // GA4 Measurement Protocol
34
+ _ga4: {
35
+ measurementId: 'G-LPV8HY4JPQ',
36
+ apiSecret: 'hNzENdMxTcOJTA4COfzEFA',
37
+ clientId: null
38
+ },
39
+
33
40
  // Estado interno
34
41
  _iframe: null,
35
42
  _modal: null,
@@ -576,6 +583,11 @@ const mkfashion = {
576
583
  this._triggerCallback('onError', data)
577
584
  break
578
585
 
586
+ case 'analytics_event':
587
+ this._log('Evento analytics', data)
588
+ this._sendToGA4(data.event_name, data.params)
589
+ break
590
+
579
591
  default:
580
592
  this._log('Acao desconhecida:', action)
581
593
  }
@@ -591,6 +603,53 @@ const mkfashion = {
591
603
  this._messageHandler = null
592
604
  this._log('Listener de mensagens removido')
593
605
  }
606
+ },
607
+
608
+ // ============ GA4 MEASUREMENT PROTOCOL ============
609
+
610
+ _getOrCreateClientId() {
611
+ if (this._ga4.clientId) {
612
+ return this._ga4.clientId
613
+ }
614
+
615
+ // Tenta pegar do localStorage
616
+ let clientId = localStorage.getItem('mkfashion_ga4_client_id')
617
+
618
+ if (!clientId) {
619
+ // Gera um novo client_id (formato: timestamp.random)
620
+ clientId = `${Date.now()}.${Math.floor(Math.random() * 1000000000)}`
621
+ localStorage.setItem('mkfashion_ga4_client_id', clientId)
622
+ }
623
+
624
+ this._ga4.clientId = clientId
625
+ return clientId
626
+ },
627
+
628
+ async _sendToGA4(eventName, params = {}) {
629
+ try {
630
+ const url = `https://www.google-analytics.com/mp/collect?measurement_id=${this._ga4.measurementId}&api_secret=${this._ga4.apiSecret}`
631
+
632
+ const payload = {
633
+ client_id: this._getOrCreateClientId(),
634
+ events: [{
635
+ name: eventName,
636
+ params: {
637
+ ...params,
638
+ engagement_time_msec: 100
639
+ }
640
+ }]
641
+ }
642
+
643
+ const response = await fetch(url, {
644
+ method: 'POST',
645
+ body: JSON.stringify(payload)
646
+ })
647
+
648
+ this._log('GA4 evento enviado:', eventName, response.status)
649
+
650
+ } catch (error) {
651
+ console.error('[mKFashion] Erro ao enviar para GA4:', error)
652
+ }
594
653
  }
595
654
  }
596
655