1"use strict";(self.webpackChunkpixelpoint_website=self.webpackChunkpixelpoint_website||[]).push([[9371],{6539:function(e,t,o){o.r(t),o.d(t,{Head:function(){return v},default:function(){return y}});var n=o(8453),a=o(6540),r=o(4754);const s={annotations:r.hk,Code:r.Cy},i={staticMediaQuery:"not screen, (max-width: 768px)",lineNumbers:void 0,showCopyButton:!0,themeName:"dark-plus"};function l(e){const t=Object.assign({p:"p",ol:"ol",li:"li",code:"code",ul:"ul",span:"span",h2:"h2",a:"a"},(0,n.RP)(),e.components),{Video:o}=t;return s||A("CH",!1),s.Code||A("CH.Code",!0),o||A("Video",!0),a.createElement(a.Fragment,null,a.createElement("style",{dangerouslySetInnerHTML:{__html:'[data-ch-theme="dark-plus"] { --ch-t-colorScheme: var(--ch-0);--ch-t-foreground: var(--shiki-token-embed);--ch-t-background: var(--shiki-background-editor);--ch-t-editor-background: var(--shiki-background-editor);--ch-t-editor-foreground: var(--shiki-token-embed);--ch-t-tab-activeBackground: var(--shiki-background-editor);--ch-t-tab-inactiveBackground: var(--shiki-background-editor);--ch-t-tab-border: var(--shiki-menu-background);--ch-t-tab-activeBorder: var(--shiki-background-editor);--ch-t-editorGroupHeader-tabsBackground: var(--shiki-background-header);--ch-t-input-foreground: var(--shiki-token-embed);--ch-t-sideBar-foreground: var(--shiki-token-embed); }'}}),"\n",a.createElement(t.p,null,"Dark mode has become increasingly popular in recent years, as it can help reduce eyestrain and save battery life on devices. If you have a website or an app, you may want to track whether your users are using dark mode so that you can better understand their preferences and tailor your content accordingly."),"\n",a.createElement(t.p,null,"To track the user's dark mode setting, we will use Google Analytics 4 (GA4) and Google Tag Manager (GTM). This process will include the configuration of GTM, the creation of a custom dimension in GA, and, finally, setting up a report for gained data."),"\n",a.createElement(t.p,null,"Here are the steps to create a custom variable in GTM to track the user's dark and light theme setting:"),"\n",a.createElement(o,{autoPlay:!0,playsInline:!0,muted:!0,loop:!0,width:"1528",height:"1080"},a.createElement("source",{type:"video/mp4",src:"https://pixel-point-website.s3.amazonaws.com/posts/2022-12-12-how-to-track-user-dark-and-light-mode-in-ga4/video-1.mp4"}),a.createElement("source",{type:"video/webm",src:"https://pixel-point-website.s3.amazonaws.com/posts/2022-12-12-how-to-track-user-dark-and-light-mode-in-ga4/video-1.webm"})),"\n",a.createElement(t.ol,null,"\n",a.createElement(t.li,null,'In your GTM account, go to the "Variables" section and click on the "New" button.'),"\n",a.createElement(t.li,null,'On the "Create Variable" page, select "Custom JavaScript" as the variable type and name it âUser Color Modeâ.'),"\n",a.createElement(t.li,null,'In the "Custom JavaScript" field, enter a function that returns either "dark" or "light" depending on the user\'s dark mode setting.'),"\n",a.createElement(t.li,null,'Click on the "Save" button to save the custom variable.'),"\n"),"\n",a.createElement(s.Code,{codeConfig:i,northPanel:{tabs:[""],active:"",heightRatio:1},files:[{name:"",focus:"",code:{lines:[{tokens:[{content:" function",props:{style:{color:"var(--shiki-token-keyword)"}}},{content:" () {",props:{style:{color:"var(--shiki-token-embed)"}}}]},{tokens:[{content:" if",props:{style:{color:"var(--shiki-token-keyword)"}}},{content:" (",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"window",props:{style:{color:"var(--shiki-color-text)"}}},{content:".",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"matchMedia ",props:{style:{color:"var(--shiki-color-text)"}}},{content:"&& ",props:{style:{color:"var(--shiki-token-keyword)"}}},{content:"window",props:{style:{color:"var(--shiki-color-text)"}}},{content:".",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"matchMedia",props:{style:{color:"var(--shiki-token-function)"}}},{content:"(",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"'(prefers-color-scheme: dark)'",props:{style:{color:"var(--shiki-token-string)"}}},{content:").",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"matches",props:{style:{color:"var(--shiki-color-text)"}}},{content:") {",props:{style:{color:"var(--shiki-token-embed)"}}}]},{tokens:[{content:" return ",props:{style:{color:"var(--shiki-token-keyword)"}}}
1,{content:"'Dark'",props:{style:{color:"var(--shiki-token-string)"}}},{content:";",props:{style:{color:"var(--shiki-token-embed)"}}}]},{tokens:[{content:" }",props:{style:{color:"var(--shiki-token-embed)"}}}]},{tokens:[{content:" else if",props:{style:{color:"var(--shiki-token-keyword)"}}},{content:" (",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"window",props:{style:{color:"var(--shiki-color-text)"}}},{content:".",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"matchMedia ",props:{style:{color:"var(--shiki-color-text)"}}},{content:"&& ",props:{style:{color:"var(--shiki-token-keyword)"}}},{content:"window",props:{style:{color:"var(--shiki-color-text)"}}},{content:".",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"matchMedia",props:{style:{color:"var(--shiki-token-function)"}}},{content:"(",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"'(prefers-color-scheme: light)'",props:{style:{color:"var(--shiki-token-string)"}}},{content:").",props:{style:{color:"var(--shiki-token-embed)"}}},{content:"matches",props:{style:{color:"var(--shiki-color-text)"}}},{content:") {",props:{style:{color:"var(--shiki-token-embed)"}}}]},{tokens:[{content:" return ",props:{style:{color:"var(--shiki-token-keyword)"}}},{content:"'Light'",props:{style:{color:"var(--shiki-token-string)"}}},{content:";",props:{style:{color:"var(--shiki-token-embed)"}}}]},{tokens:[{content:" }",props:{style:{color:"var(--shiki-token-embed)"}}}]},{tokens:[{content:" return ",props:{style:{color:"var(--shiki-token-keyword)"}}},{content:"'No Preference'",props:{style:{color:"var(--shiki-token-string)"}}},{content:";",props:{style:{color:"var(--shiki-token-embed)"}}}]},{tokens:[{content:" }",props:{style:{color:"var(--shiki-token-embed)"}}}]}],lang:"jsx"},annotations:[]}]}),"\n",a.createElement(t.p,null,"Note: ",a.createElement(t.code,null,"window.matchMedia('(prefers-color-scheme: dark)')")," shows user preference through an operating system setting (e.g., light or dark mode) or a user agent setting. If you want to track it based on the userâs selected option in your app, you can extend this approach by adding the GA user data programmatically.(GA docs)"),"\n",a.createElement(t.p,null,"Once you have created the custom variable in GTM to track the user's dark mode setting, you can create a custom tag in GTM that sends the data to GA4 as an event. To create a custom tag in GTM, follow these steps:"),"\n",a.createElement(o,{autoPlay:!0,playsInline:!0,muted:!0,loop:!0,width:"1528",height:"1080"},a.createElement("source",{type:"video/mp4",src:"https://pixel-point-website.s3.amazonaws.com/posts/2022-12-12-how-to-track-user-dark-and-light-mode-in-ga4/video-2.mp4"}),a.createElement("source",{type:"video/webm",src:"https://pixel-point-website.s3.amazonaws.com/posts/2022-12-12-how-to-track-user-dark-and-light-mode-in-ga4/video-2.webm"})),"\n",a.createElement(t.ol,null,"\n",a.createElement(t.li,null,'In your GTM account, go to the "Tags" section and click on the "New" button.'),"\n",a.createElement(t.li,null,'In the "Create Tag" page, select "Google Analytics: GA4 Event" as the tag type.'),"\n",a.createElement(t.li,null,'In the "Google Analytics Settings" section, select the correct GA4 property from the "Configuration Tag" dropdown. Make sure that youâve selected the property that corresponds to your website or app.'),"\n",a.createElement(t.li,null,'In the "Event Parameters" section, enter the following values:',"\n",a.createElement(t.ul,null,"\n",a.createElement(t.li,null,'Event name: "page_view"'),"\n",a.createElement(t.li,null,"Event parameters: ",a.createElement(t.code,null,'{ "color_mode": {{User Color Mode}} }')),"\n"),"\n"),"\n",a.createElement(t.li,null,"Add trigger âInitialization - All pagesâ"),"\n",a.createElement(t.li,null,"Press âSaveâ"),"\n"),"\n",a.createElement(t.p,null,'Now to see the results in Google Analytics(GA4) we need to add custom dimension. You can create a custom dimension named "User Color Mode" with a user property value of "color_mode." Here are the steps to do this:'),"\n",a.createElement(o,{autoPlay:!0,playsInl
1ine:!0,muted:!0,loop:!0,width:"1528",height:"1080"},a.createElement("source",{type:"video/mp4",src:"https://pixel-point-website.s3.amazonaws.com/posts/2022-12-12-how-to-track-user-dark-and-light-mode-in-ga4/video-3.mp4"}),a.createElement("source",{type:"video/webm",src:"https://pixel-point-website.s3.amazonaws.com/posts/2022-12-12-how-to-track-user-dark-and-light-mode-in-ga4/video-3.webm"})),"\n",a.createElement(t.ol,null,"\n",a.createElement(t.li,null,"Sign in to your Google Analytics account and navigate to the Configure section."),"\n",a.createElement(t.li,null,"Click on Custom Definitions, then click on Custom Dimensions."),"\n",a.createElement(t.li,null,'Click the "New Custom Dimension" button.'),"\n",a.createElement(t.li,null,'In the "Name" field, enter "User Color Mode" as the name of the custom dimension.'),"\n",a.createElement(t.li,null,'In the "Scope" field, select "User" as the scope for this custom dimension. This will allow the dimension to be associated with individual users of your website.'),"\n",a.createElement(t.li,null,'In the "Event Parameter" field, enter "color_mode" as the event parameter that will set the value of this custom dimension.'),"\n",a.createElement(t.li,null,'In the "Active" field, make sure youâve checked the box to enable this custom dimension.'),"\n",a.createElement(t.li,null,'Click the "Create" button to save the new custom dimension.'),"\n"),"\n",a.createElement(t.p,null,'Once you\'ve set up, you can immediately view the statistics on the "Reports" â "Realtime" page. However, on other pages such as "Explore," you\'ll need to wait 24 hours before you can start using this new user property.'),"\n",a.createElement(t.span,{dangerouslySetInnerHTML:{__html:'<span\n class="gatsby-resp-image-wrapper"\n style="position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 696px; "\n >
1\n <span\n class="gatsby-resp-image-background-image"\n style="padding-bottom: 71.26436781609195%; position: relative; bottom: 0; left: 0; background-image: url(\'data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAQFA//EABYBAQEBAAAAAAAAAAAAAAAAAAIAA//aAAwDAQACEAMQAAABcYlMummRuv/EABoQAAIDAQEAAAAAAAAAAAAAAAIDAAEEBRT/2gAIAQEAAQUCQkCR51zYNA9O4FqroBc0tprf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAGBEAAgMAAAAAAAAAAAAAAAAAAAECISL/2gAIAQIBAT8B0RdH/8QAHRAAAgIBBQAAAAAAAAAAAAAAAQIAERIhMTIzUf/aAAgBAQAGPwJCVG3k61lKKFRVKtoJwaZAVP/EABgQAAMBAQAAAAAAAAAAAAAAAAABESHh/9oACAEBAAE/IXYbeqNSnNBiGDTlVDhBZZIlp//aAAwDAQACAAMAAAAQ5N//xAAWEQEBAQAAAAAAAAAAAAAAAAABABH/2gAIAQMBAT8QxWC//8QAFxEBAQEBAAAAAAAAAAAAAAAAAREAIf/aAAgBAgEBPxCp46Au/8QAHBABAQEAAQUAAAAAAAAAAAAAAREAIVFhcaHR/9oACAEBAAE/EBHTVRXzccJn1mbUqjC4YjGkPvCMCd/rI+BJN43/2Q==\'); background-size: cover; display: block;"\n ></span>\n <picture>\n <source\n srcset="/static/20261ae1a2387f0a90eb053e7652d66b/09400/screenshot-1.webp 174w,\n/static/20261ae1a2387f0a90eb053e7652d66b/3266f/screenshot-1.webp 348w,\n/static/20261ae1a2387f0a90eb053e7652d66b/657d2/screenshot-1.webp 696w,\n/static/20261ae1a2387f0a90eb053e7652d66b/e644e/screenshot-1.webp 1044w,\n/static/20261ae1a2387f0a90eb053e7652d66b/8daba/screenshot-1.webp 1392w,\n/static/20261ae1a2387f0a90eb053e7652d66b/bf8d8/screenshot-1.webp 3496w"\n sizes="(max-width: 696px) 100vw, 696px"\n type="image/webp"\n />\n <source\n srcset="/static/20261ae1a2387f0a90eb053e7652d66b/f52d2/screenshot-1.jpg 174w,\n/static/20261ae1a2387f0a90eb053e7652d66b/14ab8/screenshot-1.jpg 348w,\n/static/20261ae1a2387f0a90eb053e7652d66b/a4476/screenshot-1.jpg 696w,\n/static/20261ae1a2387f0a90eb053e7652d66b/4ef30/screenshot-1.jpg 1044w,\n/static/20261ae1a2387f0a90eb053e7652d66b/16454/screenshot-1.jpg 1392w,\n/static/20261ae1a2387f0a90eb053e7652d66b/aa759/screenshot-1.jpg 3496w"\n sizes="(max-width: 696px) 100vw, 696px"\n type="image/jpeg"\n />\n <img\n class="gatsby-resp-image-image"\n src="/static/20261ae1a2387f0a90eb053e7652d66b/a4476/screenshot-1.jpg"\n alt=""\n title=""\n loading="lazy"\n decoding="async"\n style="width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"\n />\n </picture>\n </span>'}}),"\n",a.createElement(t.h2,{id:"summary"},a.createElement(t.a,{"aria-hidden":"true",tabIndex:"-1",href:"#summary"},a.createElement(t.span,{className:"icon icon-link"})),"Summary"),"\n",a.createElement(t.p,null,"Now you know how to set up Google Analytics 4 and GTM in order to track the userâs dark or light theme setting. Analysis of this data can help you define whether itâs important to implement dark mode for the website or app for your audience."),"\n",a.createElement(t.p,null,"If you enjoyed the article and want to see more web tips, ",a.createElement(t.a,{href:"https://twitter.com/alex_barashkov"},"follow me on Twitter"),"."))}var c=function(e){void 0===e&&(e={});const{wrapper:t}=Object.assign({},(0,n.RP)(),e.components);return t?a.createElement(t,e,a.createElement(l,e)):l(e)};function A(e,t){throw new Error("Expected "+(t?"component":"object")+" `"+e+"` to be defined: you likely forgot to import, pass, or provide it.")}var d=o(2532),p=o(1021),m=o(545),h=o(9072),u=o(3669),k=o(9063),g=o(352),b=o(84);const w=e=>{let{data:{mdx:{author:t,frontmatter:o,fields:{slug:n}},allMdx:{nodes:r}},location:s,pageContext:{videoCovers:i},children:l}=e;return a.createElement(k.A,{headerTheme:"black",headerShowThemeButton:!0},a.createElement("article",{className:"safe-paddings pt-32 sm:pt-24"},a.createElement("div",{className:"container"},a.createElement("div",{className:"relative mx-auto max-w-[696px] xl:mx-0 xl:flex xl:max-w-none xl:justify-center xl:space-x-20 lg:space-x-8 md:block md:space-x-0"},a.createElement("div",{className:"xl:max-w-[696px] lg:max-w-[626px] md:max-w-none"},a.createElement(m.A,Object.assign({},o,{slug:n})),a.createElement(p.A,{content:l,videoCovers:i})),a.createElement(h.A,{author:t,readMorePosts:r,socialShareUrl:s.href})))),a.createElement(u.A,{withTopMargin:!0}))};function y(e){return a.createElement(w,e,a.createElement(c,e))}const v=e=>{let{data:{mdx:{frontmatter:{title:t,summary:o,ogImage:n}}}}=e;const r=(0,d.d)(n);return a.createElement(g.A,b.A.blogPost({title:t,description:o,ogImage:r}))}}}]); 2//# sourceMappingURL=component---src-templates-blog-post-jsx-content-file-path-content-posts-2022-12-12-how-to-track-user-dark-and-light-mode-in-ga-4-index-md-d68127fa5458a20c1fc1.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.