cjapy 0.2.3__tar.gz → 0.2.3.post2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/PKG-INFO +1 -1
- cjapy-0.2.3.post2/cjapy/__version__.py +1 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/cjapy.py +256 -3
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/requestCreator.py +211 -31
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy.egg-info/PKG-INFO +1 -1
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/cja.md +39 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/releases.md +5 -1
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/requestCreator.md +45 -1
- cjapy-0.2.3/cjapy/__version__.py +0 -1
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/LICENSE +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/README.md +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/__init__.py +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/config.py +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/configs.py +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/connector.py +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/projects.py +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/token_provider.py +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy/workspace.py +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy.egg-info/SOURCES.txt +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy.egg-info/dependency_links.txt +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy.egg-info/requires.txt +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/cjapy.egg-info/top_level.txt +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/authenticating_without_config_json.md +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/getting_started.md +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/logging.md +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/main.md +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/projects.md +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/docs/workspace.md +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/pyproject.toml +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/requirements.txt +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/setup.cfg +0 -0
- {cjapy-0.2.3 → cjapy-0.2.3.post2}/setup.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.3-2"
|
|
@@ -8,7 +8,7 @@ from collections import defaultdict, deque
|
|
|
8
8
|
import time, logging, re
|
|
9
9
|
from itertools import tee
|
|
10
10
|
from datetime import datetime, timedelta
|
|
11
|
-
|
|
11
|
+
import string
|
|
12
12
|
|
|
13
13
|
# Non standard libraries
|
|
14
14
|
import pandas as pd
|
|
@@ -2460,13 +2460,13 @@ class CJA:
|
|
|
2460
2460
|
def getFreeformTable(
|
|
2461
2461
|
self,
|
|
2462
2462
|
dimension: str = None,
|
|
2463
|
-
metrics:
|
|
2463
|
+
metrics: Union[str] = None,
|
|
2464
2464
|
dataviewId: str = None,
|
|
2465
2465
|
start_date: Union[str, datetime] = None,
|
|
2466
2466
|
end_date: Union[str, datetime] = None,
|
|
2467
2467
|
top_n: int = 400,
|
|
2468
2468
|
filterId: str = None,
|
|
2469
|
-
search: Union[str,
|
|
2469
|
+
search: Union[str, list] = None,
|
|
2470
2470
|
search_operator: str = "OR",
|
|
2471
2471
|
) -> pd.DataFrame:
|
|
2472
2472
|
"""
|
|
@@ -2560,3 +2560,256 @@ class CJA:
|
|
|
2560
2560
|
|
|
2561
2561
|
df.columns = [clean_column_name(col) for col in df.columns]
|
|
2562
2562
|
return df
|
|
2563
|
+
|
|
2564
|
+
def getPersonProfiles(
|
|
2565
|
+
self,
|
|
2566
|
+
dataviewId: str = None,
|
|
2567
|
+
personId: str = "variables/adobe_identitynamespace_personid",
|
|
2568
|
+
featureMetrics: list = None,
|
|
2569
|
+
targetMetric: str = None,
|
|
2570
|
+
binaryTargetMetric: bool = False,
|
|
2571
|
+
startDate: Union[str, datetime] = None,
|
|
2572
|
+
endDate: Union[str, datetime] = None,
|
|
2573
|
+
sampleSize: int = 50000,
|
|
2574
|
+
sampleSeed: int = 0,
|
|
2575
|
+
fullPersonHistoryOnly: bool = False,
|
|
2576
|
+
removeSingleEventPeople: bool = False,
|
|
2577
|
+
filterId: str = None
|
|
2578
|
+
) -> pd.DataFrame:
|
|
2579
|
+
"""
|
|
2580
|
+
Retrieves a dataset where every row is a person profile.
|
|
2581
|
+
|
|
2582
|
+
Arguments:
|
|
2583
|
+
dataviewId : REQUIRED : The dataView ID to use for your report.
|
|
2584
|
+
personId : OPTIONAL : The dimension to use as your person ID. Defaults to adobe_identitynamespace_personid.
|
|
2585
|
+
featureMetrics : REQUIRED : List of metrics to include in the customer profiles.
|
|
2586
|
+
targetMetric : OPTIONAL : A target metric for supervised learning algorithms.
|
|
2587
|
+
TODO: featureDimensions (only supported when "flat view" is supported)
|
|
2588
|
+
binaryTargetMetric : OPTIONAL: Sets the target metric to 1 if the metric sum for a person is > 0 otherwise sets it to 0
|
|
2589
|
+
startDate : OPTIONAL : The start date for the report in 'YYYY-MM-DD' format or datetime object (defaults to 90 days ago).
|
|
2590
|
+
endDate : OPTIONAL : The end date for the report in 'YYYY-MM-DD' format or datetime object (defaults to today).
|
|
2591
|
+
sampleSize : OPTIONAL : Number of sampled person profiles to return (default 50000). All person profiles are returned if sample is greater than the total population.
|
|
2592
|
+
sampleSeed: OPTIONAL : A seed value for the random sampling of person profiles (default 0).
|
|
2593
|
+
fullPersonHistoryOnly : OPTIONAL : Filters the sample to only people whose entire history is present in the date range. Defaults to false.
|
|
2594
|
+
removeSingleEventPeople : OPTIONAL : Removes people that had only a single event in the date range.
|
|
2595
|
+
filterId : OPTIONAL : The segment ID to filter the report with.
|
|
2596
|
+
|
|
2597
|
+
Returns:
|
|
2598
|
+
A pandas DataFrame containing the person profile data.
|
|
2599
|
+
"""
|
|
2600
|
+
|
|
2601
|
+
if featureMetrics is None:
|
|
2602
|
+
raise ValueError("Require at least one feature metric")
|
|
2603
|
+
if dataviewId is None:
|
|
2604
|
+
raise ValueError("Require a Data View ID")
|
|
2605
|
+
def ensure_dimension_prefix(dimension: str) -> str:
|
|
2606
|
+
if not dimension.startswith("variables/"):
|
|
2607
|
+
return f"variables/{dimension}"
|
|
2608
|
+
return dimension
|
|
2609
|
+
def ensure_metric_prefix(metric: str) -> str:
|
|
2610
|
+
if not metric.startswith("metrics/"):
|
|
2611
|
+
return f"metrics/{metric}"
|
|
2612
|
+
return metric
|
|
2613
|
+
def ensure_datetime_format(date: Union[str, datetime], is_start: bool) -> str:
|
|
2614
|
+
if isinstance(date, str):
|
|
2615
|
+
date = datetime.fromisoformat(date)
|
|
2616
|
+
if date.time() == datetime.min.time():
|
|
2617
|
+
if is_start:
|
|
2618
|
+
return date.strftime('%Y-%m-%dT00:00:00.000')
|
|
2619
|
+
else:
|
|
2620
|
+
return date.strftime('%Y-%m-%dT23:59:59.999')
|
|
2621
|
+
return date.strftime('%Y-%m-%dT%H:%M:%S.000')
|
|
2622
|
+
if startDate is None:
|
|
2623
|
+
startDate = datetime.today() - timedelta(days=90)
|
|
2624
|
+
if endDate is None:
|
|
2625
|
+
endDate = datetime.today()
|
|
2626
|
+
start_date_str = ensure_datetime_format(startDate, is_start=True)
|
|
2627
|
+
end_date_str = ensure_datetime_format(endDate, is_start=False)
|
|
2628
|
+
date_range = f"{start_date_str}/{end_date_str}"
|
|
2629
|
+
personId = ensure_dimension_prefix(personId)
|
|
2630
|
+
metrics = [ensure_metric_prefix(metric) for metric in featureMetrics]
|
|
2631
|
+
targetMetric = ensure_metric_prefix(targetMetric)
|
|
2632
|
+
|
|
2633
|
+
dataRequest = RequestCreator()
|
|
2634
|
+
dataRequest.setDataViewId(dataviewId)
|
|
2635
|
+
|
|
2636
|
+
sampling_metric_definition = {
|
|
2637
|
+
"func": "calc-metric",
|
|
2638
|
+
"formula": {
|
|
2639
|
+
"func": "add",
|
|
2640
|
+
"col1": {
|
|
2641
|
+
"func": "modulo",
|
|
2642
|
+
"description": "Modulo",
|
|
2643
|
+
"col1": {
|
|
2644
|
+
"func": "add",
|
|
2645
|
+
"col1": {
|
|
2646
|
+
"func": "multiply",
|
|
2647
|
+
"col1": {
|
|
2648
|
+
"func": "visualization-group",
|
|
2649
|
+
"col": {
|
|
2650
|
+
"func": "add",
|
|
2651
|
+
"col1": {
|
|
2652
|
+
"func": "cumul",
|
|
2653
|
+
"description": "Cumulative",
|
|
2654
|
+
"n": 0,
|
|
2655
|
+
"col": 1
|
|
2656
|
+
},
|
|
2657
|
+
"col2": sampleSeed
|
|
2658
|
+
}
|
|
2659
|
+
},
|
|
2660
|
+
"col2": 1664525
|
|
2661
|
+
},
|
|
2662
|
+
"col2": 1013904223
|
|
2663
|
+
},
|
|
2664
|
+
"col2": {
|
|
2665
|
+
"func": "pow",
|
|
2666
|
+
"description": "Power operator",
|
|
2667
|
+
"col1": 2,
|
|
2668
|
+
"col2": 32
|
|
2669
|
+
}
|
|
2670
|
+
},
|
|
2671
|
+
"col2": {
|
|
2672
|
+
"func": "visualization-group",
|
|
2673
|
+
"col": {
|
|
2674
|
+
"func": "multiply",
|
|
2675
|
+
"col1": {
|
|
2676
|
+
"func": "metric",
|
|
2677
|
+
"name": "metrics/occurrences",
|
|
2678
|
+
"description": "Events"
|
|
2679
|
+
},
|
|
2680
|
+
"col2": 0
|
|
2681
|
+
}
|
|
2682
|
+
}
|
|
2683
|
+
},
|
|
2684
|
+
"version": [
|
|
2685
|
+
1,
|
|
2686
|
+
0,
|
|
2687
|
+
0
|
|
2688
|
+
]
|
|
2689
|
+
}
|
|
2690
|
+
|
|
2691
|
+
dataRequest.addMetric(metricDefinition=sampling_metric_definition)
|
|
2692
|
+
|
|
2693
|
+
for metric in metrics:
|
|
2694
|
+
dataRequest.addMetric(metric)
|
|
2695
|
+
|
|
2696
|
+
if binaryTargetMetric:
|
|
2697
|
+
binary_metric_definition = {
|
|
2698
|
+
"func": "calc-metric",
|
|
2699
|
+
"formula": {
|
|
2700
|
+
"func": "if",
|
|
2701
|
+
"description": "If",
|
|
2702
|
+
"cond": {
|
|
2703
|
+
"func": "gt",
|
|
2704
|
+
"description": "Greater Than",
|
|
2705
|
+
"col1": {
|
|
2706
|
+
"func": "metric",
|
|
2707
|
+
"name": f"{targetMetric}",
|
|
2708
|
+
"description": "Binary Target Metric"
|
|
2709
|
+
},
|
|
2710
|
+
"col2": 0
|
|
2711
|
+
},
|
|
2712
|
+
"then": 1,
|
|
2713
|
+
"else": 0
|
|
2714
|
+
},
|
|
2715
|
+
"version": [
|
|
2716
|
+
1,
|
|
2717
|
+
0,
|
|
2718
|
+
0
|
|
2719
|
+
]
|
|
2720
|
+
}
|
|
2721
|
+
dataRequest.addMetric(metricDefinition=binary_metric_definition)
|
|
2722
|
+
else:
|
|
2723
|
+
dataRequest.addMetric(targetMetric)
|
|
2724
|
+
|
|
2725
|
+
dataRequest.setDimension(personId)
|
|
2726
|
+
dataRequest.setNoneBehavior(returnNones=False)
|
|
2727
|
+
|
|
2728
|
+
if filterId:
|
|
2729
|
+
dataRequest.addGlobalFilter(filterId)
|
|
2730
|
+
dataRequest.addGlobalFilter(date_range)
|
|
2731
|
+
|
|
2732
|
+
if fullPersonHistoryOnly:
|
|
2733
|
+
fullHistoryFilter = {
|
|
2734
|
+
"container": {
|
|
2735
|
+
"func": "container",
|
|
2736
|
+
"context": "visitors",
|
|
2737
|
+
"pred": {
|
|
2738
|
+
"func": "eq",
|
|
2739
|
+
"num": 1,
|
|
2740
|
+
"val": {
|
|
2741
|
+
"func": "attr",
|
|
2742
|
+
"name": "variables/adobe_firstvreturn_sessiontype"
|
|
2743
|
+
},
|
|
2744
|
+
"description": "Session Type"
|
|
2745
|
+
}
|
|
2746
|
+
},
|
|
2747
|
+
"func": "segment",
|
|
2748
|
+
"version": [
|
|
2749
|
+
1,
|
|
2750
|
+
0,
|
|
2751
|
+
0
|
|
2752
|
+
]
|
|
2753
|
+
}
|
|
2754
|
+
dataRequest.addGlobalFilter(adHocFilter=fullHistoryFilter)
|
|
2755
|
+
|
|
2756
|
+
if removeSingleEventPeople:
|
|
2757
|
+
excludeSingleEventPeopleFilter = {
|
|
2758
|
+
"container": {
|
|
2759
|
+
"func": "container",
|
|
2760
|
+
"context": "visitors",
|
|
2761
|
+
"pred": {
|
|
2762
|
+
"func": "without",
|
|
2763
|
+
"pred": {
|
|
2764
|
+
"func": "eq",
|
|
2765
|
+
"num": 1,
|
|
2766
|
+
"val": {
|
|
2767
|
+
"func": "total",
|
|
2768
|
+
"evt": {
|
|
2769
|
+
"func": "event",
|
|
2770
|
+
"name": "metrics/occurrences"
|
|
2771
|
+
}
|
|
2772
|
+
},
|
|
2773
|
+
"description": "Events"
|
|
2774
|
+
}
|
|
2775
|
+
}
|
|
2776
|
+
},
|
|
2777
|
+
"func": "segment",
|
|
2778
|
+
"version": [
|
|
2779
|
+
1,
|
|
2780
|
+
0,
|
|
2781
|
+
0
|
|
2782
|
+
]
|
|
2783
|
+
}
|
|
2784
|
+
dataRequest.addGlobalFilter(adHocFilter=excludeSingleEventPeopleFilter)
|
|
2785
|
+
|
|
2786
|
+
response = self.getReport(request=dataRequest, n_results=sampleSize)
|
|
2787
|
+
|
|
2788
|
+
df = response.dataframe
|
|
2789
|
+
|
|
2790
|
+
# Drop the itemId column if it exists
|
|
2791
|
+
if 'itemId' in df.columns:
|
|
2792
|
+
df = df.drop(columns=['itemId'])
|
|
2793
|
+
|
|
2794
|
+
# Drop the sampling metric
|
|
2795
|
+
if 'ad_hoc_cm_0' in df.columns:
|
|
2796
|
+
df = df.drop(columns=['ad_hoc_cm_0'])
|
|
2797
|
+
|
|
2798
|
+
# Remove any rows that exceed the requested top_n
|
|
2799
|
+
if len(df) > sampleSize:
|
|
2800
|
+
df = df.head(sampleSize)
|
|
2801
|
+
|
|
2802
|
+
# Clean the column names to remove prefixes
|
|
2803
|
+
def clean_column_name(col):
|
|
2804
|
+
if col.startswith("variables/"):
|
|
2805
|
+
return col.replace("variables/", "")
|
|
2806
|
+
elif col.startswith("metrics/"):
|
|
2807
|
+
return col.replace("metrics/", "")
|
|
2808
|
+
return col
|
|
2809
|
+
|
|
2810
|
+
df.columns = [clean_column_name(col) for col in df.columns]
|
|
2811
|
+
|
|
2812
|
+
# Randomize the rows
|
|
2813
|
+
df_randomized = df.sample(frac=1).reset_index(drop=True)
|
|
2814
|
+
|
|
2815
|
+
return df_randomized
|
|
@@ -2,6 +2,7 @@ from copy import deepcopy
|
|
|
2
2
|
import datetime
|
|
3
3
|
import json
|
|
4
4
|
from time import time
|
|
5
|
+
from typing import Union
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
class RequestCreator:
|
|
@@ -99,18 +100,132 @@ class RequestCreator:
|
|
|
99
100
|
def __str__(self):
|
|
100
101
|
return json.dumps(self.__request, indent=4)
|
|
101
102
|
|
|
102
|
-
def addMetric(
|
|
103
|
+
def addMetric(
|
|
104
|
+
self,
|
|
105
|
+
metricId: str = None,
|
|
106
|
+
attributionModel: str = None,
|
|
107
|
+
lookbackWindow: Union[int, str] = 30,
|
|
108
|
+
lookbackGranularity: str = "day",
|
|
109
|
+
**kwargs
|
|
110
|
+
) -> None:
|
|
103
111
|
"""
|
|
104
112
|
Add a metric to the template.
|
|
105
113
|
Arguments:
|
|
106
114
|
metricId : REQUIRED : The metric to add
|
|
115
|
+
attributionModel : OPTIONAL : The attribution model to use (e.g., "lastTouch", "firstTouch", "linear", "participation", "sameTouch", "uShaped", "jShaped", "reverseJShaped", "timeDecay", "positionBased", "algorithmic")
|
|
116
|
+
lookbackWindow : OPTIONAL : The lookback window (number of minutes, hours, days, weeks, months, quarters, "session", or "person"). Assumes 30 if not specified.
|
|
117
|
+
lookbackGranularity : OPTIONAL : The granularity of the lookback window (minute, hour, day, week, month, quarter). Defaults to "day".
|
|
118
|
+
**kwargs : Additional model-specific parameters. For "timeDecay" (assumes 1 week): halfLifeNumPeriods, halfLifeGranularity, for "positionBased": firstWeight, middleWeight, lastWeight.
|
|
107
119
|
"""
|
|
108
120
|
if metricId is None:
|
|
109
121
|
raise ValueError("Require a metric ID")
|
|
122
|
+
|
|
123
|
+
if attributionModel and attributionModel not in ["lastTouch", "firstTouch", "linear", "participation", "sameTouch", "uShaped", "jShaped", "reverseJShaped", "timeDecay", "positionBased", "algorithmic"]:
|
|
124
|
+
raise ValueError("Invalid attribution model")
|
|
125
|
+
|
|
126
|
+
if attributionModel and lookbackGranularity not in ["minute", "hour", "day", "week", "month", "quarter"]:
|
|
127
|
+
raise ValueError("Invalid lookbackGranularity. Valid values are: 'minute', 'hour', 'day', 'week', 'month', 'quarter'")
|
|
128
|
+
|
|
110
129
|
columnId = self.__metricCount
|
|
111
130
|
addMetric = {"columnId": str(columnId), "id": metricId}
|
|
112
|
-
|
|
131
|
+
|
|
132
|
+
# Only add sorting to the first metric if no dimension sort is specified
|
|
133
|
+
if columnId == 0 and "dimensionSort" not in self.__request["settings"]:
|
|
113
134
|
addMetric["sort"] = "desc"
|
|
135
|
+
|
|
136
|
+
if attributionModel:
|
|
137
|
+
# Handle sameTouch separately
|
|
138
|
+
if attributionModel == "sameTouch":
|
|
139
|
+
allocationModel = {
|
|
140
|
+
"func": "allocation-instance"
|
|
141
|
+
}
|
|
142
|
+
else:
|
|
143
|
+
allocationModel = {
|
|
144
|
+
"func": f"allocation-{attributionModel}"
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
# Validate lookback window
|
|
148
|
+
if lookbackWindow is not None and attributionModel != "sameTouch":
|
|
149
|
+
if isinstance(lookbackWindow, int):
|
|
150
|
+
if lookbackWindow <= 0:
|
|
151
|
+
raise ValueError("lookbackWindow must be positive")
|
|
152
|
+
|
|
153
|
+
# Default lookback granularity to days if not specified
|
|
154
|
+
lookbackGranularity = kwargs.get('lookbackGranularity', lookbackGranularity)
|
|
155
|
+
|
|
156
|
+
max_lookback = {
|
|
157
|
+
'minute': 129600,
|
|
158
|
+
'hour': 2160,
|
|
159
|
+
'day': 90,
|
|
160
|
+
'week': 12,
|
|
161
|
+
'month': 3,
|
|
162
|
+
'quarter': 1
|
|
163
|
+
}.get(lookbackGranularity, 90)
|
|
164
|
+
|
|
165
|
+
if lookbackWindow > max_lookback:
|
|
166
|
+
raise ValueError(f"lookbackWindow of {lookbackWindow} exceeds maximum for {lookbackGranularity} granularity")
|
|
167
|
+
|
|
168
|
+
if attributionModel not in ["lastTouch", "firstTouch"] or lookbackWindow != "session":
|
|
169
|
+
allocationModel["lookbackExpiration"] = {
|
|
170
|
+
"func": "allocation-lookbackPeriod",
|
|
171
|
+
"granularity": lookbackGranularity,
|
|
172
|
+
"numPeriods": lookbackWindow
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
addMetric["lookback"] = {
|
|
176
|
+
"func": "min-months",
|
|
177
|
+
"granularity": lookbackGranularity,
|
|
178
|
+
"numPeriods": lookbackWindow
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if attributionModel in ["lastTouch", "firstTouch"]:
|
|
182
|
+
allocationModel["expiration"] = {
|
|
183
|
+
"context": "visitors",
|
|
184
|
+
"func": "allocation-container"
|
|
185
|
+
}
|
|
186
|
+
else:
|
|
187
|
+
allocationModel["context"] = "visitors"
|
|
188
|
+
|
|
189
|
+
elif lookbackWindow == "session":
|
|
190
|
+
if attributionModel in ["lastTouch", "firstTouch"]:
|
|
191
|
+
allocationModel["expiration"] = {
|
|
192
|
+
"context": "sessions",
|
|
193
|
+
"func": "allocation-container"
|
|
194
|
+
}
|
|
195
|
+
else:
|
|
196
|
+
allocationModel["context"] = "sessions"
|
|
197
|
+
elif lookbackWindow == "person":
|
|
198
|
+
if attributionModel not in ["lastTouch", "firstTouch"]:
|
|
199
|
+
allocationModel["context"] = "visitors"
|
|
200
|
+
else:
|
|
201
|
+
raise ValueError("lookbackWindow must be a positive integer or 'session' or 'person'")
|
|
202
|
+
|
|
203
|
+
if attributionModel in ["lastTouch", "firstTouch"]:
|
|
204
|
+
allocationModel["expiration"] = {
|
|
205
|
+
"context": allocationModel.get("expiration", {}).get("context", "visitors"),
|
|
206
|
+
"func": "allocation-container"
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if attributionModel == "timeDecay":
|
|
210
|
+
halfLifeNumPeriods = kwargs.get("halfLifeNumPeriods", 1)
|
|
211
|
+
halfLifeGranularity = kwargs.get("halfLifeGranularity", "week")
|
|
212
|
+
if halfLifeNumPeriods is None or halfLifeGranularity not in ["minute", "hour", "day", "week", "month"]:
|
|
213
|
+
raise ValueError("For 'timeDecay', both 'halfLifeNumPeriods' and 'halfLifeGranularity' are required and granularity must be 'minute', 'hour', 'day', 'week', or 'month'")
|
|
214
|
+
allocationModel["halfLifeGranularity"] = halfLifeGranularity
|
|
215
|
+
allocationModel["halfLifeNumPeriods"] = halfLifeNumPeriods
|
|
216
|
+
|
|
217
|
+
if attributionModel == "positionBased":
|
|
218
|
+
firstWeight = kwargs.get("firstWeight")
|
|
219
|
+
middleWeight = kwargs.get("middleWeight")
|
|
220
|
+
lastWeight = kwargs.get("lastWeight")
|
|
221
|
+
if firstWeight is None or middleWeight is None or lastWeight is None:
|
|
222
|
+
raise ValueError("For 'positionBased', 'firstWeight', 'middleWeight', and 'lastWeight' are required")
|
|
223
|
+
allocationModel["firstWeight"] = firstWeight
|
|
224
|
+
allocationModel["middleWeight"] = middleWeight
|
|
225
|
+
allocationModel["lastWeight"] = lastWeight
|
|
226
|
+
|
|
227
|
+
addMetric["allocationModel"] = allocationModel
|
|
228
|
+
|
|
114
229
|
self.__request["metricContainer"]["metrics"].append(addMetric)
|
|
115
230
|
self.__metricCount += 1
|
|
116
231
|
|
|
@@ -301,17 +416,39 @@ class RequestCreator:
|
|
|
301
416
|
else:
|
|
302
417
|
self.__request["settings"]["nonesBehavior"] = "exclude-nones"
|
|
303
418
|
|
|
304
|
-
def
|
|
419
|
+
def setDimensionSort(self, order: str = "dsc") -> None:
|
|
420
|
+
"""
|
|
421
|
+
Set sorting based on a dimension value.
|
|
422
|
+
Arguments:
|
|
423
|
+
order : OPTIONAL : The sort order, either "asc" or "dsc" (default is "dsc").
|
|
424
|
+
"""
|
|
425
|
+
if order not in ["asc", "dsc"]:
|
|
426
|
+
raise ValueError("Sort order must be 'asc' or 'dsc'")
|
|
427
|
+
|
|
428
|
+
self.__request["settings"]["dimensionSort"] = order
|
|
429
|
+
|
|
430
|
+
# Remove sort from all metrics if dimension sort is set
|
|
431
|
+
for metric in self.__request["metricContainer"]["metrics"]:
|
|
432
|
+
if "sort" in metric:
|
|
433
|
+
del metric["sort"]
|
|
434
|
+
|
|
435
|
+
def setDimension(self, dimension: str = None, sort_order: str = None) -> None:
|
|
305
436
|
"""
|
|
306
437
|
Set the dimension to be used for reporting.
|
|
307
438
|
Arguments:
|
|
308
439
|
dimension : REQUIRED : the dimension to build your report on
|
|
440
|
+
sort_order : OPTIONAL : The sort order, either "asc" or "desc".
|
|
309
441
|
"""
|
|
310
442
|
if dimension is None:
|
|
311
443
|
raise ValueError("A dimension must be passed")
|
|
444
|
+
|
|
312
445
|
if 'dimensions' in self.__request.keys():
|
|
313
446
|
del self.__request["dimensions"]
|
|
447
|
+
|
|
314
448
|
self.__request["dimension"] = dimension
|
|
449
|
+
|
|
450
|
+
if sort_order:
|
|
451
|
+
self.setDimensionSort(sort_order)
|
|
315
452
|
|
|
316
453
|
def setIdentityOverrides(self, identity_overrides: list) -> None:
|
|
317
454
|
"""
|
|
@@ -336,47 +473,90 @@ class RequestCreator:
|
|
|
336
473
|
raise ValueError("A dataView ID must be passed")
|
|
337
474
|
self.__request["dataId"] = dataViewId
|
|
338
475
|
|
|
339
|
-
def addGlobalFilter(self, filterId: str = None) -> None:
|
|
476
|
+
def addGlobalFilter(self, filterId: str = None, adHocFilter: dict = None) -> None:
|
|
340
477
|
"""
|
|
341
478
|
Add a global filter to the report.
|
|
342
|
-
NOTE
|
|
479
|
+
NOTE: You need to have a dateRange filter at least in the global report.
|
|
480
|
+
|
|
343
481
|
Arguments:
|
|
344
|
-
filterId :
|
|
345
|
-
|
|
482
|
+
filterId : OPTIONAL : The filter to add to the global filter.
|
|
483
|
+
Example :
|
|
346
484
|
"s2120430124uf03102jd8021" -> segment
|
|
347
485
|
"2020-01-01T00:00:00.000/2020-02-01T00:00:00.000" -> dateRange
|
|
486
|
+
adHocFilter : OPTIONAL : A dictionary representing the ad-hoc filter.
|
|
348
487
|
"""
|
|
349
|
-
if filterId
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
488
|
+
if not filterId and not adHocFilter:
|
|
489
|
+
raise ValueError("You must supply either a filterId or an adHocFilter.")
|
|
490
|
+
if filterId and adHocFilter:
|
|
491
|
+
raise ValueError("You cannot supply both a filterId and an adHocFilter.")
|
|
492
|
+
|
|
493
|
+
if filterId:
|
|
494
|
+
if filterId.startswith("s") and "@AdobeOrg" in filterId:
|
|
495
|
+
filter = {
|
|
496
|
+
"type": "segment",
|
|
497
|
+
"segmentId": filterId,
|
|
498
|
+
}
|
|
499
|
+
elif filterId.startswith("20") and "/20" in filterId:
|
|
500
|
+
filter = {
|
|
501
|
+
"type": "dateRange",
|
|
502
|
+
"dateRange": filterId,
|
|
503
|
+
}
|
|
504
|
+
elif ":::" in filterId:
|
|
505
|
+
dimension, itemId = filterId.split(":::")
|
|
506
|
+
filter = {
|
|
507
|
+
"type": "breakdown",
|
|
508
|
+
"dimension": dimension,
|
|
509
|
+
"itemId": itemId,
|
|
510
|
+
}
|
|
511
|
+
else:
|
|
512
|
+
filter = {
|
|
513
|
+
"type": "segment",
|
|
514
|
+
"segmentId": filterId,
|
|
515
|
+
}
|
|
516
|
+
else:
|
|
371
517
|
filter = {
|
|
372
|
-
"type":
|
|
373
|
-
"
|
|
518
|
+
"type": "segment",
|
|
519
|
+
"segmentDefinition": adHocFilter
|
|
374
520
|
}
|
|
521
|
+
|
|
375
522
|
### incrementing the count for globalFilter
|
|
376
523
|
self.__globalFiltersCount += 1
|
|
377
524
|
### adding to the globalFilter list
|
|
378
525
|
self.__request["globalFilters"].append(filter)
|
|
379
526
|
|
|
527
|
+
def setDateRange(self, start_date: Union[str], end_date: Union[str]) -> None:
|
|
528
|
+
"""
|
|
529
|
+
Set the date range for the request.
|
|
530
|
+
Arguments:
|
|
531
|
+
start_date : REQUIRED : The start date for the report in 'YYYY-MM-DD' format or datetime object.
|
|
532
|
+
end_date : REQUIRED : The end date for the report in 'YYYY-MM-DD' format or datetime object.
|
|
533
|
+
"""
|
|
534
|
+
def ensure_datetime_format(date: Union[str], is_start: bool) -> str:
|
|
535
|
+
if isinstance(date, str):
|
|
536
|
+
date = datetime.datetime.fromisoformat(date)
|
|
537
|
+
if date.time() == datetime.datetime.min.time():
|
|
538
|
+
if is_start:
|
|
539
|
+
return date.strftime('%Y-%m-%dT00:00:00.000')
|
|
540
|
+
else:
|
|
541
|
+
return date.strftime('%Y-%m-%dT23:59:59.999')
|
|
542
|
+
return date.strftime('%Y-%m-%dT%H:%M:%S.000')
|
|
543
|
+
|
|
544
|
+
start_date_str = ensure_datetime_format(start_date, is_start=True)
|
|
545
|
+
end_date_str = ensure_datetime_format(end_date, is_start=False)
|
|
546
|
+
date_range = f"{start_date_str}/{end_date_str}"
|
|
547
|
+
|
|
548
|
+
# Update global filters with the new date range
|
|
549
|
+
date_range_filter = {
|
|
550
|
+
"type": "dateRange",
|
|
551
|
+
"dateRange": date_range
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
# Remove any existing dateRange filters before adding the new one
|
|
555
|
+
self.__request["globalFilters"] = [
|
|
556
|
+
f for f in self.__request["globalFilters"] if f["type"] != "dateRange"
|
|
557
|
+
]
|
|
558
|
+
self.__request["globalFilters"].append(date_range_filter)
|
|
559
|
+
|
|
380
560
|
def updateDateRange(
|
|
381
561
|
self,
|
|
382
562
|
dateRange: str = None,
|
|
@@ -601,4 +601,43 @@ The following arguments are possible with this method:
|
|
|
601
601
|
* countRepeatInstances : OPTIONAL : set to count repeatInstances values (or not). True by default.
|
|
602
602
|
* returnNones : OPTIONAL : Set the behavior of the None values in that request. (True by default)
|
|
603
603
|
|
|
604
|
+
|
|
605
|
+
## getPersonProfiles
|
|
606
|
+
|
|
607
|
+
The `getPersonProfiles` method retrieves a dataset where each row represents a person profile. This is particularly useful for extracting feature metrics and target metrics for supervised learning applications or customer segmentation analysis.
|
|
608
|
+
|
|
609
|
+
### Arguments:
|
|
610
|
+
- `dataviewId`: **REQUIRED**. The Data View ID to use for your report.
|
|
611
|
+
- `personId`: **OPTIONAL**. The dimension to use as your person ID. Defaults to `variables/adobe_identitynamespace_personid`.
|
|
612
|
+
- `featureMetrics`: **REQUIRED**. A list of metrics to include in the customer profiles.
|
|
613
|
+
- `targetMetric`: **OPTIONAL**. A target metric for supervised learning algorithms.
|
|
614
|
+
- `binaryTargetMetric`: **OPTIONAL**. If set to `True`, sets the target metric to 1 if the metric sum for a person is greater than 0, otherwise sets it to 0.
|
|
615
|
+
- `startDate`: **OPTIONAL**. The start date for the report in 'YYYY-MM-DD' format or as a datetime object. Defaults to 90 days ago.
|
|
616
|
+
- `endDate`: **OPTIONAL**. The end date for the report in 'YYYY-MM-DD' format or as a datetime object. Defaults to today.
|
|
617
|
+
- `sampleSize`: **OPTIONAL**. The number of sampled person profiles to return. Defaults to 50,000. If the sample size exceeds the total population, all profiles are returned.
|
|
618
|
+
- `sampleSeed`: **OPTIONAL**. A seed value for random sampling of person profiles. Defaults to 0.
|
|
619
|
+
- `fullPersonHistoryOnly`: **OPTIONAL**. If set to `True`, filters the sample to only people whose entire history is present in the date range. Defaults to `False`.
|
|
620
|
+
- `removeSingleEventPeople`: **OPTIONAL**. If set to `True`, removes people that had only a single event in the date range.
|
|
621
|
+
- `filterId`: **OPTIONAL**. The segment ID to filter the report with.
|
|
622
|
+
|
|
623
|
+
### Returns:
|
|
624
|
+
A `pandas.DataFrame` containing the person profile data.
|
|
625
|
+
|
|
626
|
+
### Example usage:
|
|
627
|
+
|
|
628
|
+
```python
|
|
629
|
+
# Assuming `cja` is an instance of the CJA class
|
|
630
|
+
feature_metrics = ["metric1", "metric2", "metric3"]
|
|
631
|
+
df_profiles = cja.getPersonProfiles(
|
|
632
|
+
dataviewId="your_data_view_id",
|
|
633
|
+
featureMetrics=feature_metrics,
|
|
634
|
+
targetMetric="target_metric",
|
|
635
|
+
startDate="2023-01-01",
|
|
636
|
+
endDate="2023-03-31",
|
|
637
|
+
sampleSize=10000,
|
|
638
|
+
binaryTargetMetric=True
|
|
639
|
+
)
|
|
640
|
+
print(df_profiles.head())
|
|
641
|
+
```
|
|
642
|
+
|
|
604
643
|
[Back to README](../README.md)
|
|
@@ -13,7 +13,11 @@ Patch:
|
|
|
13
13
|
* Fixing some edge cases of `findComponentUsage`
|
|
14
14
|
* Adding more parameters for `createDataView` methods
|
|
15
15
|
* Fixing `updateCalculatedMetric`
|
|
16
|
-
* Fixing the `setSearch
|
|
16
|
+
* Fixing the `setSearch`\
|
|
17
|
+
Patch:
|
|
18
|
+
* adding attribution model possibility to addMetric in `RequestCreator`
|
|
19
|
+
* adding `setDateRange` method in `RequestCreator`
|
|
20
|
+
* supporting adhoc filter in `RequestCreator`
|
|
17
21
|
|
|
18
22
|
## 0.2.1
|
|
19
23
|
|
|
@@ -75,6 +75,44 @@ We will review the different methods available via the object.
|
|
|
75
75
|
|
|
76
76
|
* `addMetric()`
|
|
77
77
|
Add a metric to the Request.
|
|
78
|
+
Arguments:
|
|
79
|
+
* metricId : REQUIRED : The metric to add
|
|
80
|
+
* attributionModel : OPTIONAL : The attribution model to use (e.g., "lastTouch", "firstTouch", "linear", "participation", "sameTouch", "uShaped", "jShaped", "reverseJShaped", "timeDecay", "positionBased", "algorithmic")
|
|
81
|
+
* lookbackWindow : OPTIONAL : The lookback window (number of minutes, hours, days, weeks, months, quarters, "session", or "person"). Assumes 30 if not specified.
|
|
82
|
+
* lookbackGranularity : OPTIONAL : The granularity of the lookback window (minute, hour, day, week, month, quarter). Defaults to "day".
|
|
83
|
+
* **kwargs : Additional model-specific parameters. For "timeDecay" (assumes 1 week): halfLifeNumPeriods, halfLifeGranularity; for "positionBased": firstWeight, middleWeight, lastWeight.
|
|
84
|
+
|
|
85
|
+
The `addMetric` method also supports various attribution models. For example, the `sameTouch` model is defined without a lookback window, while other models like `lastTouch` and `firstTouch` can have lookback windows and expiration settings. The `timeDecay` model allows specifying half-life periods and granularity, with defaults to 1 week if not provided.
|
|
86
|
+
|
|
87
|
+
Examples:
|
|
88
|
+
```python
|
|
89
|
+
# Adding a simple metric
|
|
90
|
+
myRequest.addMetric("metrics/orders")
|
|
91
|
+
|
|
92
|
+
# Adding a metric with an attribution model
|
|
93
|
+
myRequest.addMetric("metrics/orders", attributionModel="lastTouch", lookbackWindow=30)
|
|
94
|
+
|
|
95
|
+
# Adding a metric with a custom attribution model
|
|
96
|
+
myRequest.addMetric(
|
|
97
|
+
"metrics/orders",
|
|
98
|
+
attributionModel="timeDecay",
|
|
99
|
+
lookbackWindow=60,
|
|
100
|
+
halfLifeNumPeriods=1,
|
|
101
|
+
halfLifeGranularity="week"
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# Adding a metric with position-based attribution model
|
|
105
|
+
myRequest.addMetric(
|
|
106
|
+
"metrics/orders",
|
|
107
|
+
attributionModel="positionBased",
|
|
108
|
+
lookbackWindow=30,
|
|
109
|
+
firstWeight=30,
|
|
110
|
+
middleWeight=20,
|
|
111
|
+
lastWeight=50
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
# Adding a metric with sameTouch attribution model
|
|
115
|
+
myRequest.addMetric("metrics/orders", attributionModel="sameTouch")
|
|
78
116
|
|
|
79
117
|
* `addGlobalFilter()`
|
|
80
118
|
Add a global filter to the report.
|
|
@@ -131,6 +169,12 @@ We will review the different methods available via the object.
|
|
|
131
169
|
* filterId : REQUIRED : The filter to add.
|
|
132
170
|
when breakdown, use the following format for the value "dimension:::itemId"
|
|
133
171
|
|
|
172
|
+
* `setDateRange()`
|
|
173
|
+
Set the date range for the request.
|
|
174
|
+
Arguments:
|
|
175
|
+
* start_date : REQUIRED : The start date for the report in 'YYYY-MM-DD' format or datetime object.
|
|
176
|
+
* end_date : REQUIRED : The end date for the report in 'YYYY-MM-DD' format or datetime object.
|
|
177
|
+
|
|
134
178
|
* `updateDateRange()`
|
|
135
179
|
Update the dateRange filter on the globalFilter list
|
|
136
180
|
One of the 3 elements specified below is required.
|
|
@@ -167,4 +211,4 @@ This can help you derive other dates if needed.
|
|
|
167
211
|
|
|
168
212
|
### search
|
|
169
213
|
|
|
170
|
-
`search` attribute is a boolean defining if search has been set for the dimension.
|
|
214
|
+
`search` attribute is a boolean defining if search has been set for the dimension.
|
cjapy-0.2.3/cjapy/__version__.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.3"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|