Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.mavera.io/llms.txt

Use this file to discover all available pages before exploring further.

Scenario

Your desktop and mobile users behave differently — session lengths, engagement patterns, scroll depth, and conversion rates all vary by device. You pull device category, screen resolution, and engagement metrics from GA4, then send the behavioral breakdown to Mave for creative format recommendations. The result tells you which ad formats, content layouts, and creative dimensions to prioritize for each device segment.

Architecture

Code

import os, requests
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import (
    RunReportRequest, Dimension, Metric, DateRange, OrderBy,
)

PROPERTY_ID = os.environ["GA4_PROPERTY_ID"]
MV = os.environ["MAVERA_API_KEY"]

client = BetaAnalyticsDataClient()

device_report = client.run_report(RunReportRequest(
    property=f"properties/{PROPERTY_ID}",
    dimensions=[
        Dimension(name="deviceCategory"),
        Dimension(name="screenResolution"),
    ],
    metrics=[
        Metric(name="totalUsers"),
        Metric(name="sessions"),
        Metric(name="engagementRate"),
        Metric(name="averageSessionDuration"),
        Metric(name="conversions"),
        Metric(name="screenPageViewsPerSession"),
    ],
    date_ranges=[DateRange(start_date="30daysAgo", end_date="today")],
    order_bys=[OrderBy(metric=OrderBy.MetricOrderBy(metric_name="totalUsers"), desc=True)],
    limit=100,
))

from collections import defaultdict

devices = defaultdict(lambda: {
    "users": 0, "sessions": 0, "conversions": 0,
    "eng_sum": 0, "dur_sum": 0, "pages_sum": 0,
    "resolutions": defaultdict(int),
})

for row in device_report.rows:
    cat = row.dimension_values[0].value
    res = row.dimension_values[1].value
    users = int(row.metric_values[0].value)
    sessions = int(row.metric_values[1].value)
    engagement = float(row.metric_values[2].value)
    duration = float(row.metric_values[3].value)
    conversions = int(row.metric_values[4].value)
    pages_per = float(row.metric_values[5].value)

    devices[cat]["users"] += users
    devices[cat]["sessions"] += sessions
    devices[cat]["conversions"] += conversions
    devices[cat]["eng_sum"] += engagement * users
    devices[cat]["dur_sum"] += duration * users
    devices[cat]["pages_sum"] += pages_per * sessions
    devices[cat]["resolutions"][res] += users

device_block = []
for cat, data in sorted(devices.items(), key=lambda x: -x[1]["users"]):
    avg_eng = data["eng_sum"] / max(data["users"], 1)
    avg_dur = data["dur_sum"] / max(data["users"], 1)
    avg_pages = data["pages_sum"] / max(data["sessions"], 1)
    conv_rate = data["conversions"] / max(data["users"], 1)
    top_res = sorted(data["resolutions"].items(), key=lambda x: -x[1])[:5]
    res_str = ", ".join(f"{r}: {n}" for r, n in top_res)

    device_block.append(
        f"**{cat.upper()}**\n"
        f"  Users: {data['users']} | Sessions: {data['sessions']}\n"
        f"  Engagement: {avg_eng:.0%} | Avg duration: {avg_dur:.0f}s | Pages/session: {avg_pages:.1f}\n"
        f"  Conversions: {data['conversions']} ({conv_rate:.2%})\n"
        f"  Top resolutions: {res_str}"
    )

device_summary = "\n\n".join(device_block)

mave = requests.post(
    "https://app.mavera.io/api/v1/mave/chat",
    headers={"Authorization": f"Bearer {MV}", "Content-Type": "application/json"},
    json={"message": f"""Recommend creative format adjustments for each device category based on this GA4 behavioral data.

DEVICE BEHAVIORAL PROFILES (last 30 days):
{device_summary}

For each device category, provide:
1. Recommended ad creative dimensions and formats (static, video, carousel, etc.)
2. Optimal content layout (long-form vs. snackable, scroll depth expectations)
3. CTA placement recommendations based on session duration and pages/session
4. Landing page design considerations for the top screen resolutions
5. Content format priorities (video length, image aspect ratio, text density)
6. Specific do's and don'ts for creative on this device

Also provide cross-device recommendations:
- Which messages to keep consistent across devices
- Which elements to adapt per device
- Mobile-first vs. desktop-first content strategy recommendation"""},
).json()

print("--- Device-Specific Creative Recommendations ---")
print(mave.get("content", "")[:3000])

Example Output

--- Device-Specific Creative Recommendations ---

## Mobile (62% of users, 1.8% conv rate)
- **Ad formats:** 9:16 vertical video (15s max), story-format carousels, single image 1080×1080
- **Content layout:** Snackable — one idea per screen, bullet points over paragraphs. Your 45s avg session means they decide in the first scroll.
- **CTA placement:** Fixed bottom bar or within first viewport. With 2.1 pages/session, they won't scroll far.
- **Landing pages:** Optimize for 390×844 (iPhone 14/15). Single-column, thumb-zone CTAs, collapse feature tables into accordions.
- **Don't:** Use horizontal video, multi-column layouts, or forms with more than 3 fields.

## Desktop (31% of users, 3.4% conv rate)
- **Ad formats:** 16:9 landscape video (30-60s), comparison infographics, multi-panel carousel
- **Content layout:** Long-form works here — 3.2 min avg session and 4.8 pages/session means they're evaluating deeply. Include detailed feature tables, customer quotes, and embedded demos.
- **CTA placement:** After value demonstration (not above the fold). Desktop users scroll.
- **Landing pages:** Optimize for 1920×1080. Two-column layouts with sticky nav. Include pricing calculator or interactive demo.

## Cross-Device
- **Keep consistent:** Value proposition, brand colors, core messaging hierarchy
- **Adapt:** CTA text (mobile: "Try Free" / desktop: "Start Your 14-Day Free Trial"), content depth, form length
- **Recommendation:** Mobile-first design, desktop-enhanced. 62% of traffic is mobile, but desktop converts at nearly 2x — invest in both, but design mobile first.

Error Handling

Hundreds of unique resolutions exist. The code aggregates by device category first, then lists top 5 resolutions per category. Group similar resolutions (e.g. 1920×1080 and 1920×1200 as “Full HD”) for cleaner analysis.
If tablet traffic is under 5% of total, consider merging tablet data with desktop for persona purposes. Modern tablets render desktop-class pages.
GA4 may report smart tv or other device categories with minimal traffic. Filter these out unless you specifically target living-room experiences.

What’s Next

GA4 Integration

Back to GA4 integration overview

Acquisition Channel × Persona Mapping

Map channel-demographic pairs to personas

Audience Demographics → Persona Creation

Create personas from GA4 demographic data

Mave Agent

Full reference for POST /api/v1/mave/chat