Copy this script
// Start - ProfitMetrics - Google Ads Remarketing - v1.0
// Original Code Created by FeedArmy - Modified by ProfitMetrics.io
// Step 1: Set Google Ads Conversion ID
const GOOGLE_ADS_ID = 'AW-123456789';
// Step 2: Set Google Ads Remarketing Country code
const REMARKETING_COUNTRY_CODE = 'US';
// Step 3: Set ID_TYPE
// if your products look like shopify_US_123456789_123456798 add the word shopify
// if your products are sku's add the value 'sku' like this: const ID_TYPE = 'sku';
// if your proudcts use the variant id use the value 'variant_id' like this: const ID_TYPE = 'variant_id';
// if your proudcts use the parent id use the value 'parent_id' like this: const ID_TYPE = 'parent_id';
// Consent Control
const ID_TYPE = 'shopify';
// Consent Configuration
let customerPrivacyStatus = init.customerPrivacy;
// Default consent values
let cc_statistics = true;
let cc_marketing = true;
if (!customerPrivacyStatus ||
customerPrivacyStatus.analyticsProcessingAllowed === undefined ||
customerPrivacyStatus.marketingAllowed === undefined) {
try {
const cmp_a = await browser.cookie.get('_cmp_a');
const cookiedecoded = JSON.parse(decodeURIComponent(cmp_a));
cc_statistics = cookiedecoded?.purposes?.a ?? cc_statistics;
cc_marketing = cookiedecoded?.purposes?.m ?? cc_marketing;
} catch (e) {
// Handle any errors that might occur
}
}
cc_statistics = customerPrivacyStatus.analyticsProcessingAllowed;
cc_marketing = customerPrivacyStatus.marketingAllowed;
// Load Google Tag
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', GOOGLE_ADS_ID);
const script = document.createElement('script');
script.setAttribute('src', 'https://www.googletagmanager.com/gtag/js?id='+GOOGLE_ADS_ID);
script.setAttribute('async', '');
document.head.appendChild(script);
gtag('consent', 'update', {
'ad_storage': cc_marketing ? 'granted' : 'denied',
'ad_user_data': cc_marketing ? 'granted' : 'denied',
'ad_personalization': cc_marketing ? 'granted' : 'denied',
'analytics_storage': cc_statistics ? 'granted' : 'denied'
});
const createLineItemsData = (lineItems, ID_TYPE, REMARKETING_COUNTRY_CODE) => {
return lineItems.map((item) => {
let itemId;
if (ID_TYPE === "shopify") {
itemId = "shopify_" + REMARKETING_COUNTRY_CODE + "_" + item.variant?.product?.id + "_" + item.variant?.id;
} else if (ID_TYPE === "parent_id" ) {
itemId = item.variant?.product?.id;
} else if (ID_TYPE === "sku" ) {
itemId = item.variant?.sku;
} else {
itemId = item.variant?.id;
}
return {
id: itemId
};
});
};
// testing thedata: JSON.stringify(event.data, null, 2),
analytics.subscribe("checkout_completed", (event) => {
const lineItemsData = createLineItemsData(event.data?.checkout?.lineItems, ID_TYPE, REMARKETING_COUNTRY_CODE);
gtag('event', 'purchase', {
send_to: GOOGLE_ADS_ID,
value: event.data?.checkout?.totalPrice?.amount,
currency: event.data?.checkout?.currencyCode,
google_business_vertical: 'retail',
items: lineItemsData
});
});
analytics.subscribe("product_added_to_cart", (event) => {
let itemId;
if (ID_TYPE === "shopify") {
itemId = "shopify_" + REMARKETING_COUNTRY_CODE + "_" + event.data?.cartLine?.merchandise?.product?.id + "_" + event.data?.cartLine?.merchandise?.id;
} else if (ID_TYPE === "parent_id" ) {
itemId = event.data?.cartLine?.merchandise?.product?.id;
} else if (ID_TYPE === "sku" ) {
itemId = event.data?.cartLine?.merchandise?.sku;
} else {
itemId = event.data?.cartLine?.merchandise?.id;
}
gtag('event', 'add_to_cart', {
send_to: GOOGLE_ADS_ID,
items: [{id: itemId,google_business_vertical: 'retail'}],
value: event.data?.cartLine?.merchandise?.price?.amount,
});
});
analytics.subscribe("product_viewed", (event) => {
let itemId;
if (ID_TYPE === "shopify") {
itemId = "shopify_" + REMARKETING_COUNTRY_CODE + "_" + event.data?.productVariant?.product?.id + "_" + event.data?.productVariant?.id;
} else if (ID_TYPE === "parent_id" ) {
itemId = event.data?.productVariant?.product?.id;
} else if (ID_TYPE === "sku" ) {
itemId = event.data?.productVariant?.sku;
} else {
itemId = event.data?.productVariant?.id;
}
gtag('event', 'view_item', {
send_to: GOOGLE_ADS_ID,
items: [{id: itemId,google_business_vertical: 'retail'}],
value: event.data?.productVariant?.price?.amount,
});
});
analytics.subscribe("search_submitted", (event) => {
gtag('event', 'view_search_results', {
send_to: GOOGLE_ADS_ID,
query: event.data?.searchResult?.query,
});
});
analytics.subscribe("collection_viewed", (event) => {
gtag('event', 'view_item_list', {
send_to: GOOGLE_ADS_ID,
collection_id: event.data?.collection?.id,
collection_title: event.data?.collection?.title,
});
});
// End - ProfitMetircs - Google Ads Remarketing