14. Monetization & Analytics

Defold Platformer Framework
# 14. Monetization & Analytics ## Introduction Monetization and analytics are crucial aspects of game development, especially for indie developers looking to sustain their work and understand player behavior. This section covers how to implement monetization strategies and analytics tools in your Defold platformer game. ## Monetization Strategies There are several approaches to monetizing your platformer game: 1. **Premium Model** - Charge upfront for the game 2. **Free-to-Play with In-App Purchases** - Offer the game for free with optional purchases 3. **Ad-Supported** - Display advertisements within the game 4. **Hybrid Models** - Combine multiple approaches ### Implementing In-App Purchases Defold provides native extensions for implementing in-app purchases across different platforms. ```mermaid flowchart TD A[Initialize IAP] --> B[Define Products] B --> C[Set Up Store Listeners] C --> D[Handle Purchases] D --> E[Validate Transactions] E --> F[Deliver Content] ``` #### Example IAP Implementation ```lua -- Initialize IAP local iap = require("in-app-purchases.iap") function init() -- Set up listener iap.set_listener(iap_listener) -- List of products to sell local products = { "com.yourgame.coinpack1", "com.yourgame.removeads", "com.yourgame.characterpack" } -- Load product data iap.list(products) end function iap_listener(self, transaction, error) if error then print("IAP error: " .. error.error) return end if transaction.state == iap.TRANS_STATE_PURCHASING then print("Purchasing...") elseif transaction.state == iap.TRANS_STATE_PURCHASED then -- Handle successful purchase if transaction.ident == "com.yourgame.coinpack1" then add_coins(100) elseif transaction.ident == "com.yourgame.removeads" then disable_ads() end -- Finish the transaction iap.finish(transaction) end end ``` ### Ad Integration Advertisements can be integrated using various ad networks. Defold provides extensions for popular ad providers like AdMob. #### Setting Up AdMob ```lua -- Require the AdMob module local admob = require("admob.admob") function init() -- Initialize with your app ID admob.initialize({ app_id = "ca-app-pub-XXXXXXXXXXXXXXXX~YYYYYYYYYY" }) -- Load a banner ad admob.load_banner({ id = "banner", size = admob.SIZE_BANNER, position = admob.POS_BOTTOM_CENTER, ad_unit_id = "ca-app-pub-XXXXXXXXXXXXXXXX/ZZZZZZZZZZ" }) -- Load an interstitial ad admob.load_interstitial({ id = "interstitial", ad_unit_id = "ca-app-pub-XXXXXXXXXXXXXXXX/ZZZZZZZZZZ" }) -- Load a rewarded ad admob.load_rewarded({ id = "rewarded", ad_unit_id = "ca-app-pub-XXXXXXXXXXXXXXXX/ZZZZZZZZZZ" }) end -- Show interstitial at appropriate moments (between levels, after game over) function show_interstitial() if admob.is_interstitial_loaded("interstitial") then admob.show_interstitial("interstitial") end end -- Show rewarded ads when player chooses to watch for a reward function show_rewarded_ad() if admob.is_rewarded_loaded("rewarded") then admob.show_rewarded("rewarded", function(self, event, reward_type, reward_amount) if event == admob.EVENT_CLOSED then -- Ad was closed elseif event == admob.EVENT_REWARDED then -- Player earned a reward give_reward(reward_type, reward_amount) end end) end end ``` ## Analytics Implementation Analytics help you understand player behavior, identify issues, and optimize your game. ### Common Analytics Metrics 1. **User Acquisition** - Where players come from 2. **Retention** - How often players return 3. **Session Length** - How long players play 4. **Progression** - How far players get in the game 5. **Monetization** - How players spend money ### Implementing Game Analytics Defold supports various analytics platforms. Here's an example using GameAnalytics: ```lua local gameanalytics = require("gameanalytics.gameanalytics") function init() -- Initialize GameAnalytics gameanalytics.initialize({ game_key = "YOUR_GAME_KEY", secret_key = "YOUR_SECRET_KEY", build = "1.0.0" }) -- Start a session gameanalytics.start_session() end -- Track level start function track_level_start(level_number) gameanalytics.add_progression_event({ status = gameanalytics.PROGRESSION_STATUS_START, progression01 = "World_" .. math.floor(level_number / 10), progression02 = "Level_" .. level_number }) end -- Track level complete function track_level_complete(level_number, score, time) gameanalytics.add_progression_event({ status = gameanalytics.PROGRESSION_STATUS_COMPLETE, progression01 = "World_" .. math.floor(level_number / 10), progression02 = "Level_" .. level_number, score = score, time = time }) end -- Track in-game economy function track_resource_event(currency_type, amount, item_type, item_id) gameanalytics.add_resource_event({ flow_type = gameanalytics.RESOURCE_FLOW_SINK, -- or SOURCE for gaining resources currency = currency_type, amount = amount, item_type = item_type, item_id = item_id }) end ``` ## A/B Testing A/B testing allows you to test different features or designs with different user groups to see which performs better. ```mermaid flowchart LR A[Define Test Groups] --> B[Implement Variations] B --> C[Collect Data] C --> D[Analyze Results] D --> E[Implement Winner] ``` ### Example A/B Testing Implementation ```lua function setup_ab_testing() -- Randomly assign user to a test group local user_id = get_user_id() local test_group = hash_user_to_group(user_id, 2) -- 2 groups: A and B if test_group == 0 then -- Group A: Original difficulty set_game_difficulty(1.0) analytics.track_event("ab_test", "group_a", "difficulty_normal") else -- Group B: Easier difficulty set_game_difficulty(0.8) analytics.track_event("ab_test", "group_b", "difficulty_easier") end end function hash_user_to_group(user_id, num_groups) -- Simple hash function to deterministically assign users to groups local hash = 0 for i = 1, #user_id do hash = hash + string.byte(user_id, i) end return hash % num_groups end ``` ## Balancing Monetization and Player Experience It's crucial to balance monetization with player experience. Here are some guidelines: 1. **Non-intrusive ads** - Place ads at natural break points 2. **Fair IAP pricing** - Price items reasonably 3. **No pay-to-win** - Avoid giving paying players unfair advantages 4. **Value proposition** - Ensure purchases provide clear value 5. **Alternative progression** - Allow free players to progress through effort ## GDPR and Privacy Considerations When implementing analytics and monetization, you must consider privacy regulations: ```lua function request_tracking_permission() if system.get_sys_info().system_name == "iPhone OS" then if tracking.status() == tracking.AUTHORIZATION_STATUS_NOT_DETERMINED then tracking.request_authorization(function(self, status) if status == tracking.AUTHORIZATION_STATUS_AUTHORIZED then initialize_analytics() else initialize_analytics_with_limited_tracking() end end) else -- Handle already determined status local status = tracking.status() if status == tracking.AUTHORIZATION_STATUS_AUTHORIZED then initialize_analytics() else initialize_analytics_with_limited_tracking() end end else -- For Android or other platforms show_privacy_consent_dialog() end end ``` ## Conclusion Effective monetization and analytics implementation can help sustain your game development while providing valuable insights into player behavior. By carefully balancing monetization strategies with player experience and respecting privacy considerations, you can create a successful and sustainable platformer game. In the next section, we'll explore publishing your game to various platforms and marketplaces.