Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve installers tracking #95

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
119 changes: 119 additions & 0 deletions buildResources/scripts/tracking.nsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Tracking Functions
!ifndef TRACKING_NSH
!define TRACKING_NSH

# Get Segment Write Key from environment during build
!define SEGMENT_WRITE_KEY "4gsiGKen1LyWATLxpZpsGI9iGYyAEBAF"

# Registry keys
!define TRACKING_REG_KEY "Software\Decentraland\Launcher\Analytics"
!define ANONYMOUS_ID_REG_NAME "AnonymousId"

# Function to get or create anonymous ID
Function GetAnonymousId
Push $R0
Push $R1

# Try to read from registry first
ReadRegStr $R0 HKCU "${TRACKING_REG_KEY}" "${ANONYMOUS_ID_REG_NAME}"
StrCmp $R0 "" 0 read_success

# If not found, generate new UUID and store it
nsExec::ExecToStack 'powershell -Command "[guid]::NewGuid().ToString()"'
Pop $0 # Return value
Pop $R0 # UUID string

# Create registry key and store UUID
CreateDirectory "$LOCALAPPDATA\Decentraland\Launcher"
WriteRegStr HKCU "${TRACKING_REG_KEY}" "${ANONYMOUS_ID_REG_NAME}" "$R0"
Goto done

read_success:
done:
Pop $R1
Exch $R0 # Return UUID on stack
FunctionEnd

Function IdentifyUser
Push $R1
Push $R2
Push $R3

# Get or create anonymous ID
Call GetAnonymousId
Pop $R1

# Use build-time Segment Write Key
StrCpy $R2 "${SEGMENT_WRITE_KEY}"

# Get app version from env or fallback
ReadEnvStr $R3 "APP_VERSION"
StrCmp $R3 "" 0 +2
StrCpy $R3 "unknown"

# Use PowerShell to make the HTTP request
nsExec::ExecToStack 'powershell -Command "Invoke-RestMethod -Uri https://api.segment.io/v1/identify -Method Post -ContentType application/json -Body (@{\"writeKey\"=\"$R2\";\"anonymousId\"=\"$R1\"} | ConvertTo-Json);"'
Pop $0
Pop $1

Pop $R3
Pop $R2
Pop $R1
FunctionEnd

Function TrackEvent
# Parameters are passed through $R0 (event name)
Exch $R0
Push $R1
Push $R2
Push $R3

# Use build-time Segment Write Key
StrCpy $R2 "${SEGMENT_WRITE_KEY}"

# Get or create anonymous ID
Call GetAnonymousId
Pop $R1

# Get app version from env or fallback
ReadEnvStr $R3 "APP_VERSION"
StrCmp $R3 "" 0 +2
StrCpy $R3 "unknown"

# Use PowerShell to make the HTTP request
nsExec::ExecToStack 'powershell -Command "Invoke-RestMethod -Uri https://api.segment.io/v1/track -Method Post -ContentType application/json -Body (@{\"writeKey\"=\"$R2\";\"anonymousId\"=\"$R1\";\"event\"=\"$R0\"} | ConvertTo-Json);"'
Pop $0
Pop $1

Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd

!macro IdentifyInstallation
Call IdentifyUser
!macroend

!macro TrackInstallationStart
!insertmacro IdentifyInstallation
Push "installation_started"
Call TrackEvent
!macroend

!macro TrackInstallationSuccess
Push "installation_successful"
Call TrackEvent
!macroend

!macro TrackInstallationFailed
Push "installation_failed"
Call TrackEvent
!macroend

!macro TrackInstallationAborted
Push "installation_aborted"
Call TrackEvent
!macroend

!endif # TRACKING_NSH
21 changes: 18 additions & 3 deletions buildResources/scripts/windowsInstaller.nsh
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
!include "${BUILD_RESOURCES_DIR}\scripts\tracking.nsh"

# Called at the very start of the installer runtime
!macro preInit
; Track installation start only once
!insertmacro TrackInstallationStart
!macroend

# Called after all installation steps are completed
!macro customInstall
${ifNot} ${isUpdated}
# Makes the app runs as admin
; Ensure app runs as admin
WriteRegStr HKLM "Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$INSTDIR\${APP_FILENAME}.exe" "RUNASADMIN"

# Delete old registry if exists
; Clean up old registry keys
DeleteRegKey HKCR "decentraland"
${endIf}

; Track successful installation
!insertmacro TrackInstallationSuccess
!macroend

!ifdef BUILD_UNINSTALLER
; Custom uninstaller logic
Function un.customAtomicRMDir
Exch $R0
Push $R1
Expand Down Expand Up @@ -66,6 +79,7 @@
FunctionEnd
!endif

# Remove files during uninstallation
!macro customRemoveFiles
${if} ${isUpdated}
CreateDirectory "$PLUGINSDIR\old-install"
Expand All @@ -77,7 +91,7 @@
${if} $R0 != 0
DetailPrint "File is busy, aborting: $R0"

# Attempt to restore previous directory
; Attempt to restore previous directory
Push ""
Call un.restoreFiles
Pop $R0
Expand All @@ -89,6 +103,7 @@
${endif}
!macroend

# Custom uninstaller logic
!macro customUnInstall
${ifNot} ${isUpdated}
DeleteRegKey HKCR "decentraland"
Expand Down
Loading